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

发布于 2024-08-20 20:31:52 字数 1536 浏览 7 评论 0原文

我使用以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

长不大的小祸害 2024-08-27 20:31:52

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

You have to send { dataType: 'text' } to have the success function work with jQuery and empty responses.

时光匆匆的小流年 2024-08-27 20:31:52

解决方案:

尽管我仍然无法弄清楚如何从以前的代码中获取错误,但我得到了这个适合我的替代解决方案。 (至少现在是这样)。

希望听到更多的想法,

谢谢大家

$.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);
            //                }
        });

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

$.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);
            //                }
        });
橙味迷妹 2024-08-27 20:31:52

发生这种情况并不是因为没有内容的 201 一定被认为是无效的,而是因为解析空字符串 ("") 是一个 JSON 解析错误。

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

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

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.

$.ajaxSetup({
    dataFilter: function(data, dataType) {
        if (dataType == 'json' && data == '') {
            return null;
        } else {
            return data;
        }
    }
});
三五鸿雁 2024-08-27 20:31:52

我之前就遇到过这种情况。我的问题是在查询字符串末尾没有包含“.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.

两个我 2024-08-27 20:31:52

我使用 jQuery_2.1.1 并遇到类似的问题 PUT 转到 error() 处理程序。我的 REST api 调用主体被

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

Http 状态 204 是 NoContent,所以 jQuery 对此没问题,但 201=Created 期待 json 正文对象,但我返回零长度身体部分。资源地址在位置响应标头中给出。

我可能会返回 json 对象,具体取决于少数极端情况,因此必须使用 datatype:"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 );
    }
});     

I use jQuery_2.1.1 and had similar problem PUT went to error() handler. My REST api call principal is

  • PUT to named key path (/rest/properties/mykey1): 204=Updated existing object, 201=Created new object and return Location header, XXX=anything else most like an error
  • POST to unknown key path (/rest/properties): 201=Created new object and return Location header, XXX=anything else most like an error

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.

$.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 );
    }
});     
反差帅 2024-08-27 20:31:52

我已经在其他类似线程上回答了这个问题:

我遇到了同样的问题,为我解决的一件事是未设置“dataType”。当您执行此操作时,jQuery 将尝试猜测服务器返回的数据类型,并且当您的服务器返回没​​有内容的 201 时,不会抛出错误。

I've answered this on other similar thread:

I had the same problem and one thing that solved it for me was leaving the "dataType" unsetted. When you do this jQuery will try to guess the data type the server is returning and will not throw an error when your server returns a 201 with no content.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文