数据插入成功但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"
I used following jQuery to insert data via Data Service.
Event though I got status-response 201 and the data is successfully inserted into my database, the system still regard it as an error and gives me "failed" alert?
What am I missing here?
$.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");
}
});
UPDATE:
Debug Message from 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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您必须发送 { dataType: 'text' } 才能让成功函数与 jQuery 和空响应一起使用。
You have to send { dataType: 'text' } to have the success function work with jQuery and empty responses.
解决方案:
尽管我仍然无法弄清楚如何从以前的代码中获取错误,但我得到了这个适合我的替代解决方案。 (至少现在是这样)。
希望听到更多的想法,
谢谢大家
Solution:
even though I still cant work out how I getting error from previous code, I got this alternative solution which works for me. (at least for now).
would love to hear more ideas
thank you all
发生这种情况并不是因为没有内容的 201 一定被认为是无效的,而是因为解析空字符串 ("") 是一个 JSON 解析错误。
通过设置 dataFilter 可以全局或按请求更改此行为。
This happens not because a 201 with no content is necessarily considered invalid but because parsing the empty string ("") is a JSON parse error.
This behavior can be changed globally or per-request by setting a dataFilter.
我之前就遇到过这种情况。我的问题是在查询字符串末尾没有包含“.json”扩展名,所以我得到了 XML。 jQuery 在尝试将 xml 解析为 json 时卡住了,导致错误处理程序被调用。
I had this happen to me earlier. My problem was not including a '.json' extension at the end of my query string, so I was getting XML back. jQuery choked on trying to parse the xml as json, causing the error handler to get called.
我使用 jQuery_2.1.1 并遇到类似的问题 PUT 转到 error() 处理程序。我的 REST api 调用主体被
Http 状态 204 是 NoContent,所以 jQuery 对此没问题,但 201=Created 期待 json 正文对象,但我返回零长度身体部分。资源地址在位置响应标头中给出。
我可能会返回 json 对象,具体取决于少数极端情况,因此必须使用 datatype:"json" 属性。我的 jQuery 解决方案是 error() 检查 201 状态并调用 success() 函数。
I use jQuery_2.1.1 and had similar problem PUT went to error() handler. My REST api call principal is
Http status 204 is NoContent so jQuery was fine with it, but 201=Created was expecting json body object but I returned zero length body part. Resource address was given in a location response header.
I may return json object depending on few corner cases so had to use datatype:"json" attribute. My jQuery solution was error() check for 201 status and call success() function.
我已经在其他类似线程上回答了这个问题:
I've answered this on other similar thread: