jQuery 似乎没有自动解析 JSON

标签 jquery json string parsing

这是我的客户端 jQuery 代码:

$.ajaxSetup ({
   contentType: "application/json",
   datatype: 'json'
});

$.ajax({
   type: "POST",
   url: "http://localhost:1234/path",
   data: JSON.stringify(myData),
   success: function(aString){
      alert(aString);
   },
   error: function(errorData){
      alert(errorData);
   }
});

这是服务器发出的数据:

200
Content-Type: application/json

"aStringsData"

在警报中显示“aStringData”的引号。但是,我希望由于自动 JSON.parse 会从数据类型:“json”中删除引号。难道我的理解有误吗?

最佳答案

参数实际上是dataType,而不是datatype(JavaScript 区分大小写)。

您可以尝试:

dataType: 'json' // not datatype

在您的ajaxSetup内;

关于jQuery 似乎没有自动解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11018312/

相关文章:

javascript - 将 JavaScript 值传递给 PHP 或其他 PHP 类 (codeigniter)

JQuery Mobile 1.4 文本输入宽度

javascript - 如何更改使用append()创建的元素的下拉菜单中的文本颜色?

json - 在Alfresco 4.0中启用json回调

javascript - 在 PHP 中加密字符串并在 Node.js 中解密

javascript/Jquery 代码组织

arrays - Golang 在 json 响应中获取数组索引值

python - 优化python中的字符串替换

java - 为什么我不能在 for 循环中第一次输入另一个字符串?

Java 字符串替换不起作用