无法将json发送到服务器,发生ajax错误

时间:2014-07-05 18:29:34

标签: jquery ajax json

我在我的网站上尝试了这个代码,然后在其他网站上,然后通过控制台在jquery.com上尝试:

jQuery.ajax({
url: '/',
type: 'POST',
data: '{"test":"test"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});

$(document).ajaxError(function(e, jqXHR, ajaxSettings, thrownError){
    console.log(thrownError);
});

所以控制台会记录下来:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

我不明白为什么。看起来我的代码中的一切都是正确的。

2 个答案:

答案 0 :(得分:3)

您传递的是字符串文字而不是对象:

data: { test: 'test' },

答案 1 :(得分:1)

我认为你在第4行有一个拼写错误

尝试:

data: {test:"test"},

它应该有用 - 我希望:)

对不起,我在这里遇到了3分钟1的帖子问题而无法看到胜利的评论