jQuery Get Request on HTTP URL

前端 未结 4 1467
耶瑟儿~
耶瑟儿~ 2020-12-29 00:54

i\'ve recently tried to get some Response from an URL using jQuery. Therefore I copied a get request sample of jQuery API Get Request Tutorial into my project and tried to r

相关标签:
4条回答
  • 2020-12-29 01:19

    you just can access pages from your domain/server

    0 讨论(0)
  • 2020-12-29 01:23

    You're hitting the Same Origin Policy with regard to ajax requests.

    In a nutshell, JS/Ajax is by default only allowed to fire requests on the same domain as where the HTML page is been served from. If you intend to fire requests on other domains, it has to support JSONP and/or to set the Access-Control headers in order to get it to work. If that is not an option, then you have to create some proxy on the server side and use it instead (be careful since you can be banned for leeching too much from other sites using a robot).

    0 讨论(0)
  • 2020-12-29 01:28

    I have provided an example scenario to help get you started:

    <!-- Include this jQuery library in your HTML somewhere: -->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script
    

    This is probably best to include inside of an external JS file:

    //Listen when a button, with a class of "myButton", is clicked
    //You can use any jQuery/JavaScript event that you'd like to trigger the call
    $('.myButton').click(function() {
    //Send the AJAX call to the server
      $.ajax({
      //The URL to process the request
        'url' : 'page.php',
      //The type of request, also known as the "method" in HTML forms
      //Can be 'GET' or 'POST'
        'type' : 'GET',
      //Any post-data/get-data parameters
      //This is optional
        'data' : {
          'paramater1' : 'value',
          'parameter2' : 'another value'
        },
      //The response from the server
        'success' : function(data) {
        //You can use any jQuery/JavaScript here!!!
          if (data == "success") {
            alert('request sent!');
          }
        }
      });
    });
    
    0 讨论(0)
  • 2020-12-29 01:36

    As others have said you can't access files on another server. There is a hack tho. If you are using a server side language (as i assume you are) you can simply do something like:

    http://myserver.com/google.php:

    <?php
      echo get_file_contents('http://www.google.com');
    ?>
    

    http://myserver.com/myscript.js

    $.get('google.php',function(data){ console.log(data) });
    

    That should work!

    0 讨论(0)
提交回复
热议问题