view raw
lewsid lewsid - 7 months ago 38
Ajax Question

Cross Domain Ajax Request with JQuery/PHP

Help, if you can-

The situation: includes a remotely hosted javacript file (

The goal is to just get an alert from the remotely hosted php script on

I have tried the following code in stuff.js:

type: "GET",
url: "",
dataType: 'jsonp',
success: function(result) { alert(result); }

No luck.

function(data) { alert(data); }

Also no luck.

On the php side I have tried both the following:

return json_encode(array(0 => 'test'));

echo json_encode(array(0 => 'test'));

In Firefox I get a security error. I understand that it thinks I'm violating the security model. However, according to the jquery documentation, I should be able to accomplish this.


The error seems to be a security feature of the Same Origin Policy: to simplify, you can only make AJAX requests for stuff on the originating server ( One way around this is to make a simple facade on the originating server, e.g.:

 // this file resides at
 echo file_get_contents(''
          . $possibly_some_other_GET_parameters );

Then, from, you can make an AJAX request for (which in turn makes a HTTP GET request from your web server to and sends it back to the browser).

To the browser, the request goes to the origin server, and is allowed (the browser has no way of knowing that the response comes from somewhere else behind the scene).


  • the PHP config at must have allow_url_fopen set to "1". Although this is the default setting, some servers have it disabled.
  • the request to is made from server, not from the browser. That means no cookies or user authentication data are sent to, just whatever you put into the request URL ("$possibly_some_other_GET_parameters").