简体   繁体   English

jQuery ajax-即使成功运行也会运行错误

[英]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: 请测试一些事情:

  1. Set type: "GET" instead of post. 设置type: "GET"而不是发布。 Look at this: GET OR POST 看一下: GET或POST

  2. 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 }, 
  3. Test another word instead of data to avoid conflict: 测试另一个词而不是数据,以避免冲突:

     success: function (response) 

暂无
暂无

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

相关问题 jQuery ajax:即使响应正常,错误也会运行200 - jQuery ajax:error runs even if the response is OK 200 即使代码已从Chrome开发者控制台成功运行,在freecodecamp上的测试运行仍然失败? - Test run on freecodecamp is failing even though code runs successfully from Chrome developer console? 即使响应http状态为200,也会执行jQuery ajax错误回调 - Jquery ajax error callback is executed even though the response http status is 200 即使成功执行后,Ajax jQuery也不会调用成功 - Ajax jquery not calling success even after successfully executing jquery ajax方法被重复,即使单击事件不正确 - jquery ajax method being repeated even though click event not correct 即使firefox说代码200,JQuery.AJAX也会失败 - JQuery.AJAX fails even though firefox says code 200 jquery ajax在直接执行时返回成功,但在附加到按钮时返回错误,即使服务器响应为200 OK - jquery ajax returns success when directly executed, but returns error when attached to button, even though server response is 200 OK Mongo DB; 邮递员给出错误,即使用户对象成功更新 - Mongo DB; Postman giving error even though user object successfully updates Ajax post 总是会导致错误,即使一切看起来都是正确的 - Ajax post always results in error, even though everything seems correct 为什么JQuery Ajax成功返回但在PHP应用程序的Firebug中显示错误? - Why is JQuery Ajax returning successfully but showing an error in firebug in PHP app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM