数据插入成功但jquery仍然返回错误

我使用以下jQuery通过数据服务插入数据。 事件虽然我得到状态响应201并且数据已成功插入我的数据库,但系统仍将其视为错误并给我“失败”警报?

我在这里想念的是什么?

$.ajax({ type: "POST", url: "http://localhost:49223/Form/WebDataService.svc/XMLForm(guid'1eaef3a0-d6df-45bf-a8f6-3e7292c0d77e')/XMLRecord/", data: JSON.stringify(record), contentType: "application/json; charset=utf-8", dataType: "json", success: function() { alert("Success"); }, error: function(xhr) { alert("fail"); } }); 

更新:

来自Fire Bug的调试消息:

 Preferences POST http://localhost:49223/Form/WebDataService.svc/X...ef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/ POST http://localhost:49223/Form/WebDataService.svc/XMLForm(guid%271eaef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/ jquery....min.js (line 127) POST http://localhost:49223/Form/WebDataService.svc/X...ef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/ POST http://localhost:49223/Form/WebDataService.svc/XMLForm(guid%271eaef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/ 201 Created 6.7s POST http://localhost:49223/Form/WebDataService.svc/X...ef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/ POST http://localhost:49223/Form/WebDataService.svc/XMLForm(guid%271eaef3a0-d6df-45bf-a8f6-3e7292c0d77e%27)/XMLRecord/ 201 Created get readyState 4 get responseText "{ "d" : {\r\n"__metadata"...\')/XMLForm"\r\n}\r\n}\r\n} }" get responseXML null get status 201 get statusText "Created" 

您必须发送{dataType:’text’}才能使成功函数与jQuery和空响应一起使用。

解:

即使我仍然无法解决我如何从以前的代码中得到错误,我得到了这个替代解决方案,对我有用。 (至少现在(是)。

我很想听到更多的想法

谢谢你们

 $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", dataType: "json", url: "http://localhost:49223/Form/WebDataService.svc/XMLForm(guid'1eaef3a0-d6df-45bf-a8f6-3e7292c0d77e')/XMLRecord/", data: JSON.stringify(record), complete: function(xhr) { if (xhr.readyState == 4) { if (xhr.status == 201) { alert("Created"); } } else { alert("NoGood"); } } // // success: function(data) { // alert("Success"); // }, // error: function(xhr) { // alert("fail" + xhr); // } }); 

这不是因为没有内容的201必然被认为是无效的,而是因为解析空字符串(“”)是JSON解析错误。

通过设置dataFilter,可以全局或按请求更改此行为。

 $.ajaxSetup({ dataFilter: function(data, dataType) { if (dataType == 'json' && data == '') { return null; } else { return data; } } }); 

我之前发生过这件事。 我的问题是在我的查询字符串末尾没有包含’.json’扩展名,所以我收回了XML。 尝试将xml解析为json时jQuery被阻塞,导致error handling程序被调用。

我使用jQuery_2.1.1并有类似的问题PUT去了error()处理程序。 我的REST api调用主体是

  • PUT到命名密钥路径(/ rest / properties / mykey1):204 =更新现有对象,201 =创建新对象并返回Location头,XXX =其他任何最像错误的东西
  • POST到未知密钥路径(/ rest / properties):201 =创建新对象并返回Location头,XXX =其他任何最像错误的东西

Http状态204是NoContent所以jQuery很好用,但201 = Created期待json身体对象,但我返回零长度的身体部位。 资源地址在位置响应头中给出。

我可能会返回json对象,具体取决于几个极端情况,因此必须使用数据类型:“json”属性。 我的jQuery解决方案是error()检查201状态并调用success()函数。

 $.ajax({ url: "/myapp/rest/properties/mykey1", data: jsonObj, type: "PUT", headers: { "Accept":"application/json", "Content-Type":"application/json" }, timeout: 30000, dataType: "json", success: function(data, textStatus, xhr) { data="Server returned " + xhr.status; if (xhr.status==201) data+=" "+xhr.getResponseHeader("location"); $("#ajaxreply").text(data); }, error: function(xhr, textStatus, ex) { if (xhr.status==201) { this.success(null, "Created", xhr); return; } $("#ajaxreply").text( textStatus + "," + ex + "," + xhr.responseText ); } }); 

我在其他类似的post上回答了这个问题:

我遇到了同样的问题,有一件事让我解决了这个问题就是让“dataType”无法解决。 当你这样做时,jQuery将尝试猜测服务器返回的数据类型,并且当服务器返回没​​有内容的201时不会抛出错误。