jQuery getJSON to external PHP page jQuery getJSON to external PHP page ajax ajax

jQuery getJSON to external PHP page


It could be that you haven't got a callback in test.php. Also, json_encode only accepts an array:

$results = array("key" => "value");echo $_GET['callback'] . '(' . json_encode($results) . ')';// the callback stuff is only needed if you're requesting from different domains

jQuery automatically switches to JSONP (i.e. using script tags instead of XMLHttpRequest) when you use http://. If you have test.html and test.php on the same domain, try using relative paths (and no callbacks).


Be careful with moff's answer. It's got a common XSS vulnerability: http://www.metaltoad.com/blog/using-jsonp-safely


The simplest solution would be to add the below code before any output to your test.php file, then you have more flexibility with what methods you use, a standard ajax call should work.

header ('Access-Control-Allow-Origin: *');

However, use the json callback thing when your getting data from a server beyond your control.