jQuery的,JSON和Apache问题 [英] jQuery, JSON and Apache problem

查看:108
本文介绍了jQuery的,JSON和Apache问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jQuery的JSON请求,加载一些JSON从另一台服务器(前foo.com):

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); });

和收到不错的JSON对象。所以,我觉得有问题的后端,阿帕奇2.2.14。以下是HTTP头,从服务器发送的:

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

标头是在每种情况下相同的:普通的HTTP请求或AJAX。不过,我收到内容为空与AJAX和JSON正常使用浏览器请求。我使用萤火做检查,PHP5形成JSON。

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!

推荐答案

我是pretty的肯定,为了做到跨域调用,例如这一点,你必须有一个回调,这是我们所需要做的JSONP。

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.

这里是JSONP的 http://www.insideria.com/2009/03/what-in-the-heck-is-jsonp-and.html

有关JSONP工作,你必须有一个回调的服务器来包装的JSON字符串,例如:

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){});

下面一个回调函数的jQuery产生并传递到请求,所以这将是这样的:

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}");

,其中在该函数的参数是实际JSON字符串

where the parameter in that function is the actual json string.

在PHP返回这一点,会是这样的:

in the php to return this it would be something like:

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

这篇关于jQuery的,JSON和Apache问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆