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

时间:2010-02-10 00:23:35

标签: jquery ajax wcf-data-services

我使用以下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"

6 个答案:

答案 0 :(得分:61)

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

答案 1 :(得分:11)

解决方案:

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

很想听到更多的想法

谢谢大家

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

答案 2 :(得分:4)

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

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

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

答案 3 :(得分:2)

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

答案 4 :(得分:2)

我使用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 );
    }
});     

答案 5 :(得分:2)

我已在其他similar thread上回答了这个问题:

  

我遇到了同样的问题,有一件事让我解决了这个问题就是离开" dataType" unsetted。执行此操作时,jQuery将尝试猜测服务器返回的数据类型,并且在服务器返回201但没有内容时不会抛出错误。