Problems with jQuery, JSON and Apache

I have a jQuery JSON request that downloads some JSON from another server (e.g. foo.com):

$.getJSON("http://foo.com/json.php",function(data) { alert(data); });

But I get the data as null. This is not a problem between domains, I tried the following:

$.getJSON("http://twitter.com/users/usejquery.json?callback=?",
    function(data) { alert(data); });

and got a nice JSON object. So, I think there is a problem with backend, Apache 2.2.14. Here are the HTTP headers sent from the 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 every case: a regular HTTP request or AJAX. But I get empty content with AJAX and plain JSON with a browser request. I use Firebug for tests, PHP5 for generating JSON.

Does anyone have any ideas? Thank!

+3
source share
2 answers

, , , , , JSONP.

jsonp http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html

jsonp , json . :

$.getJSON("http://foo.com/json.php?callback=?", function(data){});

jquery , - :

http://foo.com/json.php?callback=generatedFunction

:

generatedFunction("{key:value, key2:value2}");

json.

php, , - :

$callback = $_GET['callback'];
print($callback."(".json_encode($theobject).");");
+4

JSON throw JSONP jQuery.

0

Source: https://habr.com/ru/post/1735797/


All Articles