Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can AJAX request data from a remote server?

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.

like image 750
Francisc Avatar asked Oct 09 '10 19:10

Francisc


2 Answers

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
  },
});
like image 56
balexandre Avatar answered Oct 11 '22 00:10

balexandre


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.

like image 31
BalusC Avatar answered Oct 11 '22 00:10

BalusC