代码之家  ›  专栏  ›  技术社区  ›  D.J

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

  •  19
  • D.J  · 技术社区  · 16 年前

    我使用以下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 回复  |  直到 9 年前
        1
  •  61
  •   Ruben Stolk    13 年前

    必须发送{datatype:'text'}才能让success函数处理jquery和空响应。

        2
  •  11
  •   D.J    16 年前

    解决方案:

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

    想听听更多的想法

    谢谢大家

    $.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);
                //                }
            });
    
        3
  •  4
  •   Steve    9 年前

    发生这种情况并不是因为没有内容的201必然被视为无效,而是因为解析空字符串(“”)是一个json解析错误。

    通过设置datafilter,可以全局或每个请求更改此行为。

    $.ajaxSetup({
        dataFilter: function(data, dataType) {
            if (dataType == 'json' && data == '') {
                return null;
            } else {
                return data;
            }
        }
    });
    
        4
  •  2
  •   YourParadigm    16 年前

    我之前就有过这种事。我的问题是在查询字符串的末尾没有包含一个“.json”扩展名,所以我得到了XML。jquery无法将xml解析为json,导致调用错误处理程序。

        5
  •  2
  •   Whome    11 年前

    我使用jquery_2.1.1,遇到了类似的问题,将其放入error()处理程序。我的rest api调用主体是

    • 放置到命名密钥路径(/rest/properties/mykey1):204=已更新现有对象,201=已创建新对象并返回位置头,xxx=任何其他最像错误的内容
    • post to unknown key path(/rest/properties):201=创建了新对象并返回位置头,xxx=任何最像错误的内容

    http status 204不是内容,因此jquery可以使用它,但是201=created需要json body对象,但我返回了零长度的body部分。资源地址在位置响应头中给定。

    我可能会返回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 );
        }
    });     
    
        6
  •  2
  •   Community Mohan Dere    9 年前

    我已经在另一个上面回答过了 similar thread :

    我也遇到了同样的问题,有一件事帮我解决了,那就是让“数据类型”保持未设置状态。执行此操作时,jquery将尝试猜测服务器返回的数据类型,并且在服务器返回没有内容的201时不会抛出错误。