Can AJAX request data from a remote server?

后端 未结 2 1990
野趣味
野趣味 2020-11-30 04:43

Can I use XMLHttpRequests in JavaScript to request a file on a different server than the one from where the request was made?

Thank you.

相关标签:
2条回答
  • 2020-11-30 05:02

    You need to use a method that is called as JSONP.

    One of the best ways is to use jQuery to reduce the code and worries between your page and the server, and all you need to do is:

    $.ajax({
      dataType: 'jsonp',
      data: 'id=10',
      jsonp: 'jsonp_callback',
      url: 'http://myotherserver.com/getdata',
      success: function () {
        // do stuff
      },
    });
    
    0 讨论(0)
  • 2020-11-30 05:02

    Only if the remote server supports JSONP or HTTP Access-Control headers.

    Public JSON API's (like the ones provided by Google.com, Facebook.com, etc) often do.

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