Reputation: 972
I have an jQuery JSON request, that loads some JSON from another server (ex. foo.com):
$.getJSON("http://foo.com/json.php",function(data) { alert(data); });
But I receive data as null. This is not cross-domain issue, I tried following:
$.getJSON("http://twitter.com/users/usejquery.json?callback=?",
function(data) { alert(data); });
and received nice JSON object. So, I think there is problem with backend, Apache 2.2.14. Here are HTTP headers, sent from server:
Date: Sun, 07 Mar 2010 16:08:38 GMT
Server: Apache/2.2.14 (CentOS)
X-Powered-By: PHP/5.3.1
Content-Length: 2
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: application/json; charset=UTF-8
The headers are the same in each case: regular HTTP-request or AJAX. But I receive empty content with AJAX, and normal JSON with browser request. I'm using Firebug for tests, PHP5 for forming JSON.
Somebody have any ideas? Thank you!
Upvotes: 3
Views: 3804
Reputation: 83699
I'm pretty sure that in order to do cross domain calls like this you have to have a callback, it's what's needed to do JSONP.
here is some more info on jsonp http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html
For jsonp to work you have to have a callback for the server to wrap the json string in. for example:
$.getJSON("http://foo.com/json.php?callback=?", function(data){});
here a callback function is generated by jquery and passed into the request, so it would be something like:
http://foo.com/json.php?callback=generatedFunction
then what's returned by the server should be:
generatedFunction("{key:value, key2:value2}");
where the parameter in that function is the actual json string.
in the php to return this it would be something like:
$callback = $_GET['callback'];
print($callback."(".json_encode($theobject).");");
Upvotes: 4