admin管理员组

文章数量:1389975

I want to load data from this url: /?s=aaa using Javascript.

This url returns data in my browser as shown below:

Here is the code I tried:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" src = ".1.3/jquery.min.js"></script>
   </head>
   <body>
      <div id= "stage"></div>
        <script type = "text/javascript" language = "javascript">
         var url = "/?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 /?s=aaa&_=1465298988417. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

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

This url returns data in my browser as shown below:

Here is the code I tried:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" src = "http://ajax.googleapis./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..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..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.

Share Improve this question edited Jun 7, 2016 at 16:38 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Jun 7, 2016 at 11:18 Ta No BiTa No Bi 3632 gold badges7 silver badges15 bronze badges 1
  • 2 Read about jQuery.ajax() here api.jquery./jquery.ajax. – void Commented Jun 7, 2016 at 11:19
Add a ment  | 

5 Answers 5

Reset to default 2

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

To allow cross-origin resource sharing, you should set following header on the requested server http://gateway.fpts..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.

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

In addition to @Orions ment (https://stackoverflow./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.>

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./jquery.ajax/)

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

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

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

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

Make sure to include jquery if you use this.

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..vn/monitor/realtime/?s=aaa", function(data) {        
          console.log(data);
          // do something with data....        
      });
  });
})(jQuery || window.jQuery);

本文标签: How to get data request from link url in javascriptStack Overflow