Ta No Bi
Ta No Bi

Reputation: 363

How to get data request from link url in javascript?

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.

Upvotes: 0

Views: 12544

Answers (5)

Ajeet Shah
Ajeet Shah

Reputation: 19863

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.

Upvotes: 2

olezhek
olezhek

Reputation: 58

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/)

Upvotes: 1

Rohit416
Rohit416

Reputation: 3496

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);

Upvotes: 0

Bj&#248;rn Nyborg
Bj&#248;rn Nyborg

Reputation: 993

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.

Upvotes: 0

Dhaval Soni
Dhaval Soni

Reputation: 305

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);
});

Upvotes: 0

Related Questions