0

I want to load data from this url: http://gateway.fpts.com.vn/monitor/realtime/?s=aaa using Javascript.

This url returns data in my browser as shown below:

Screenshot - data as shown in a web browser

Here is the code I tried:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
   </head>
   <body>
      <div id= "stage"></div>
        <script type = "text/javascript" language = "javascript">
         var url = "http://gateway.fpts.com.vn/monitor/realtime/?s=aaa"
        $.ajax({
          url: url,
          cache: false,
          method: "GET",
        })
          .done(function( html ) {
            $( "#stage" ).append( html );
          });
      </script>
   </body>
</html>

But I am getting this Error:

XMLHttpRequest cannot load http://gateway.fpts.com.vn/monitor/realtime/?s=aaa&_=1465298988417. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

1

5 Answers 5

2

You are making a cross-origin http request as you are requesting data from a domain http://gateway.fpts.com.vn which is different form that of your html file shown in question, say it http://www.requesting-server.com

To allow cross-origin resource sharing, you should set following header on the requested server http://gateway.fpts.com.vn.

To allow requests from any domain, use * as a wildcard (less secure):

Access-Control-Allow-Origin: *

or

To allow requests from a particular domain:

Access-Control-Allow-Origin: http://www.requesting-server.com

You can also check this post which addresses the same issue.

Sign up to request clarification or add additional context in comments.

5 Comments

I tried same: developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS error 25 Uncaught ReferenceError: handler is not defined
Can you tell me how you are setting the Access-Control-Allow-Origin: * header at the server http://gateway.fpts.com.vn? You can share code / screenshot or relevant information in the question. Do not delete the original question text. Just at the bottom add details with a "Edit:" note.
Which means that I have to set up header on the server, the server accepts CORS returns response?
Hi @Orions! I use MVC. I write a function getData: make request with WebRequest, and use StreamReader read data form url. I pass data from controller to view :). It same: stackoverflow.com/questions/10565090/…
Orions! Thank you so much!
1

In addition to @Orions comment (https://stackoverflow.com/a/37678620/633781), you need to provide following header in your request to a server, to make it work:

Origin: <your domain, e.g. http://example.com>

As far as I remember, jQuery does this automatically for you. Nevertheless, you can check the request headers in your browser's Dev tools to make sure it does. You can add your own headers to requests made with jQuery, though (https://api.jquery.com/jquery.ajax/)

1 Comment

You are right in "jQuery does this automatically for you"
0

You have to import jquery library from below link to use this code. https://jquery.com/download/

$.get( "http://gateway.fpts.com.vn/monitor/realtime/?s=aaa", function( data ) {
  console.log(data);
});

2 Comments

Hi @Dhval thank you for answer! But have a error:"XMLHttpRequest cannot load gateway.fpts.com.vn/monitor/realtime/?s=aaa&_=1465298988417. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."
Please prefer this for No 'Access-Control-Allow-Origin' stackoverflow.com/questions/28547288/…
0

You can make an ajax request an then get the data, something like this:

$.ajax({
  url: "http://gateway.fpts.com.vn/monitor/realtime/?s=aaa",
}).done(function(data) {
  $("body").html(data);
});

Make sure to include jquery if you use this.

2 Comments

Hi @Bjørn Nyborg thank you for answer! But have a error:"XMLHttpRequest cannot load gateway.fpts.com.vn/monitor/realtime/?s=aaa&_=1465298988417. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access."
That is because its a cross-domain request. Try adding dataType: 'jsonp',
0

Because your data is in JSON form, you can use a shorthand for that using $.getJson(). Download jQuery library and include on top of every script. Then place the following snippet in a separate script below the jQuery library.

(function($){
  $(function(){
      $.getJson( "http://gateway.fpts.com.vn/monitor/realtime/?s=aaa", function(data) {        
          console.log(data);
          // do something with data....        
      });
  });
})(jQuery || window.jQuery);

3 Comments

Hi @Rohit416 thank you for answer! but have error: Uncaught TypeError: $.getJson is not a function
Have you included jQuery on top?
I included on top.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.