[英]jQuery ajax - error runs even though it ran successfully
I have the following code as aprt of my .ajax section 我将以下代码作为我的.ajax部分的aprt
success: function (data) {
alert("success");
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
}
The first alert never runs, however the data is submitted correctly using the below: 第一个警报永远不会运行,但是可以使用以下方式正确提交数据:
data: JSON.stringify({ "solution": JSON.stringify(data) }), // Data is HTML
In fact, the second alert comes back with a status of 200 and everything through Google Chrome console looks fine. 实际上,第二个警报的状态为200,并且通过Google Chrome控制台显示的一切都很好。
Any idea? 任何想法? Full code: 完整代码:
var request = jQuery.ajax({
url: "/answers/"+content_id,
type: "POST",
data: JSON.stringify({ "solution": data }),
dataType: "json",
headers: {
Authorization: 'Basic XXX',
'X-HTTP-Method-Override': 'PATCH',
'Content-Type': 'application/json'
},
success: function (data) {
alert("success");
},
error: function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
}
});
The $.ajax function expects JSON data as response. $ .ajax函数期望JSON数据作为响应。 If the response is not JSON, the error callback will be called. 如果响应不是JSON,则将调用错误回调。 Please have a look at what you are sending out from server. 请查看您从服务器发送的内容。
Please test some things: 请测试一些事情:
Set type: "GET"
instead of post. 设置type: "GET"
而不是发布。 Look at this: GET OR POST 看一下: GET或POST
Headers data are string (name/value), and maybe your data encoding is utf8 so set 标头数据是字符串(名称/值),也许您的数据编码是utf8,所以设置为
headers: { 'Authorization': 'Basic XXX', //high recommended 'X-HTTP-Method-Override': 'PATCH', 'Content-Type': "application/json; charset=utf-8" //low },
Test another word instead of data to avoid conflict: 测试另一个词而不是数据,以避免冲突:
success: function (response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.