如何使用JQuery处理Ajax http post的成功与失败?

时间:2014-02-23 14:22:02

标签: java jquery json spring jackson

我用这样的方式用JQuery 2.0.3做一篇http帖子:

    $.ajax({
        type: "POST",
        url: "/demo/submitTransactions",
        data: JSON.stringify({ ConfirmedTransactions: confirmedTransactions }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){alert(data);},
        failure: function(errMsg) {
            alert(errMsg);
        }
    });

json在服务器上发布和写入但响应success:failure:未启用AFAIK。接收json数据的控制器代码是

    @RequestMapping(value = "/submitTransactions", method = RequestMethod.POST, headers = {"content-type=application/json"})
    @ResponseBody
    @Transactional
    public String submitTransactions(@RequestBody @Valid final String string) {
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
            out.println("\n"+string);
            out.close();
        } catch (IOException e) {
            LOGGER.severe("IOException");
            e.printStackTrace();
        }
        LOGGER.info("Received transactions "+string);
        return "Received JSON data";
    }

我怎样才能成功收到已成功收到或未能提交的json回调?它可以提交数据但是来自服务器"Received JSON data"的响应没有做任何事情,这与void的控制器方法之间没有区别,只是写入没有{{1}的数据}}声明。您是否可以建议改进,以便发布数据的javascript也会收到响应,如果出现服务器错误,我认为该响应状态为200 OK或状态为500或类似情况?

根据@Oleg Estekhin的回答,它应该更像是这样:

return

现在,如果我还使用jackson将控制器代码更改为响应对象

@RequestMapping(value =“/ submitTransactions2”,method = RequestMethod.POST,headers = {“content-type = application / json”}) @ResponseBody @Transactional public AccountResponse submitTransactions2(@RequestBody @Valid final String string){     尝试{         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename,true)));         通过out.println( “\ n” 个+串);         out.close();     } catch(IOException e){         LOGGER.severe( “IOException异常”);         e.printStackTrace();     }     LOGGER.info(“收到的交易”+字符串);     返回新的AccountResponse(“收到的交易”,“已收到”,“收到”,“收到”); }

然后我收到回复“完成!”从服务器(但是当我只使用一个字符串而没有杰克逊时,我得到了响应“失败”。

enter image description here

根据第二个答案进行更新后,此更改似乎有效:

    $.ajax({
        type: "POST",
        url: "/demo/submitTransactions",
        data: JSON.stringify({ ConfirmedTransactions: confirmedTransactions }),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    })
            .done(function(data){alert('Done!'+data);})
            .fail(function(errMsg) {alert('Failure!'+errMsg);});

有效。 enter image description here ives返回 $.ajax({ type: "POST", url: "/demo/submitTransactions", data: JSON.stringify({ ConfirmedTransactions: confirmedTransactions }), contentType: "application/json; charset=utf-8", dataType: "text" }) .done(function(data){alert('Done!'+data);}) .fail(function(errMsg) {alert('Failure!'+errMsg);});

2 个答案:

答案 0 :(得分:2)

首先,根据jQuery ajax文档,包含失败回调的属性称为“错误”,因此您的示例应如下所示:

$.ajax({
    // other properties
    success: function(data){alert(data);},
    error: function(errMsg) {alert(errMsg);}
});

其次,自jQuery 1.8以来,“成功”,“错误”和“完整”回调被认为已被弃用,您应该使用promise-style变体:

$.ajax({
    // other properties
})
.done(function(data){alert(data);})
.fail(function(errMsg) {alert(errMsg);});

答案 1 :(得分:1)

正如Oleg Estekhin的回答所述,正确使用的属性是error。您还应该使用承诺变体:

$.ajax({ ... })
 .done(...)
 .fail(...);

接下来,您应该将dataType更改为text,以便jQuery知道返回的值将被解析为普通文本而不是JSON。这将导致调用成功回调。最终的代码将如下所示:

    $.ajax({
        type: "POST",
        url: "/demo/submitTransactions",
        data: JSON.stringify({ ConfirmedTransactions: confirmedTransactions }),
        contentType: "application/json; charset=utf-8",
        dataType: "text"
    })
     .done(function(data){alert(data);})
     .fail(function(errMsg) {alert(errMsg);})
     ;