简体   繁体   English

jQuery AJAX试图甚至使用“ dataType”:“ JSON”来解析XML

[英]JQuery AJAX trying to parse XML even with “dataType” : “JSON”

As the title suggests, I have the following: 如标题所示,我有以下内容:

$.ajax({
  "url" : ...
, "type" : "GET"
, "dataType" : "JSON"
, "success" : function(response_data) {
    that.data = response_data;
    success(response_data);
  }
, "onerror" : function(data) {
    console.log(JSON.stringify(data));
  }
});

But when I run it, I get 但是当我运行它时,我得到了

XML Parsing Error: syntax error Location: moz-nullprincipal

Looking at firebug, I see that the request was 看着萤火虫,我发现请求是

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Cache-Control   max-age=0
Connection  keep-alive
Host    localhost:8888
Referer http://localhost:8888/
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101       Firefox/13.0.1
X-Requested-With    XMLHttpRequest

Any ideas? 有任何想法吗?

Change "JSON" to lowercase "json" first. 首先将“ JSON”更改为小写的“ json”。 But this is the type that jQuery is expecting back from the server, it's not necessarily what the server will send. 但这是jQuery期望服务器返回的类型,它不一定是服务器将发送的内容。

Are you sure that the server is returning JSON? 确定服务器正在返回JSON吗? Worth a double-check. 值得仔细检查。 Have you looked at the response in Fiddler? 您是否看过Fiddler的回复? If it's XML it's the server's issue, not the script's. 如果是XML,则是服务器的问题,而不是脚本的问题。

Ok great thanks to everyone tried to answer this. 好的,非常感谢大家尝试回答这个问题。

But it turns out, as usual, I noob'ed out here. 但是事实证明,和往常一样,我不在这里。

The XML parsing error shown in firebug is not actually an error with the code, and firebug's failed attempt to guess it's XML and parse it. firebug中显示的XML解析错误实际上不是代码错误,并且firebug尝试猜测其XML并进行解析失败。

My problem was elsewhere in the program and it was solved. 我的问题在程序的其他地方,已经解决。

Upper vs lower case "JSON" did not make a difference. 大写和小写“ JSON”没有区别。

Max 最高

The real problem is that your server is not setting the 'Content-type' header. 真正的问题是您的服务器未设置“ Content-type”标头。 Set it to 'application/json'. 将其设置为“ application / json”。

For example in node.js: 例如在node.js中:

res.setHeader('Content-type', 'application/json');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM