如何摆脱Firefox中的JSON错误

How to get Rid of the JSON Error in Firefox

本文关键字:JSON 错误 中的 Firefox 何摆脱      更新时间:2023-09-26

我的响应头是

Access-Control-Allow-Meth...    GET, POST
Access-Control-Allow-Orig...    *
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Length  81
Content-Type    text/html
Date    Mon, 26 Aug 2013 06:35:53 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive  timeout=5, max=99
Pragma  no-cache
Server  Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By    PHP/5.4.7`

请求头是

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control   no-cache
Connection  keep-alive
Content-Length  31
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  USERNAMEEMAIL=shan%40atlos.com; PHPSESSID=8asm46iltcqc9oahsbaaap1c16
Host    localhost
Pragma  no-cache
Referer http://localhost/test/
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0
X-Requested-With    XMLHttpRequest`

我在firefox中出错了"不成形",这里面有什么问题。我以json格式正确地获得了数据,但它也显示了一个非常烦人的错误

发出请求的Java脚本代码GetTopNotification并且使用类进行Ajax请求是工作区

您的响应标头不正确。

if(headers_sent()) die('Should not output data before json');
header('Content-type: application/json');
echo json_encode($data_for_json);
exit;

此外,json之前不应发送任何内容,之后也不应发送。


针对以下评论:

在php代码的某个地方,您正在输出json。但是,如前所述,您的响应标头不正确:Content-type部分应设置为application/json;上面的代码就是这样做的。逐行走线:

  1. 检查你是否还没有发送任何东西,如果发送了就死亡
  2. 将响应标头的Content-type部分设置为适当的mime类型
  3. 输出json(按照目前的情况,应该可以)
  4. 退出

更多更新irt评论

您正在手动创建json字符串:我可以全心全意地建议您不要这样做,使用数组或对象,然后使用json_encode来创建json。我还在代码中添加了output_braining,以防万一。

试试看,新代码在这里


更新三个

在work-space.js中替换此行

self.responseValue = self.getValueFromResponse( jqXHR );

有了这个

if(type != 'xml') self.responseValue = data;
else self.responseValue = self.getValueFromResponse( jqXHR );

保存它,清除缓存,然后重试。