struts2的Ajax错误结果 [英] Ajax error result with struts2

查看:79
本文介绍了struts2的Ajax错误结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在struts.xml中有此配置

<action name="updateAction"
    class="it.myApp.action.ajax.UpdateActionAction">
<result name="success" type="json"/>
<result name="error" type="json"/>
</action>

这是我的动作课

...
try{
    act.update();
}catch(Exception e){
    Logger.print(MessageType.ERROR,"Update failed "+e.getMessage());
    return ForwardResult.ERROR;
}
return ForwardResult.SUCCESS;

最后,这是jQuery ajax函数

$.ajax({
    url: 'updateAction.action',
    traditional:true,
    data : {
        'actionId': id,
        'actionName': name,
        'actionDescr':descr
    },
    success: function(data) {
        $('#act_'+id).html(data.name);
        $('#des_'+id).html(data.descr);
        $('#update_'+id).html(data.update);
        $('#userId_'+id).html(data.userId);

        $('#mdf_'+id).css("display","block");
        $('#save_'+id).css("display","none");   
        $('#diag_'+id).html(data.result);
    },
    error: function(data){
        alert("AZZ!");
        $('#diag_'+id).html(data.result);
    }
}); 

当操作成功更新数据时,没有问题,ajax函数执行成功"语句,但是当操作引发异常时,ajax不会执行错误"语句,而是再次执行成功" ...

为什么?

感谢前进

M.

解决方案

由于请求未生成错误,因此错误"表示服务器返回HTTP错误代码.服务器响应可能以不同方式指示错误,例如error属性.

您可以使用Firebug或类似工具检查响应代码和内容;如果您期望返回的内容以外的其他内容,请在问题中添加一些详细信息,说明返回的内容以及您希望看到的内容.

最简单的选项是设置结果的statusCodeerrorCode,如

this is my Action class

...
try{
    act.update();
}catch(Exception e){
    Logger.print(MessageType.ERROR,"Update failed "+e.getMessage());
    return ForwardResult.ERROR;
}
return ForwardResult.SUCCESS;

at last, this is jQuery ajax function

$.ajax({
    url: 'updateAction.action',
    traditional:true,
    data : {
        'actionId': id,
        'actionName': name,
        'actionDescr':descr
    },
    success: function(data) {
        $('#act_'+id).html(data.name);
        $('#des_'+id).html(data.descr);
        $('#update_'+id).html(data.update);
        $('#userId_'+id).html(data.userId);

        $('#mdf_'+id).css("display","block");
        $('#save_'+id).css("display","none");   
        $('#diag_'+id).html(data.result);
    },
    error: function(data){
        alert("AZZ!");
        $('#diag_'+id).html(data.result);
    }
}); 

when action updates data with success, there aren't problems, ajax function executes "success" statement, but when action throws an exception, ajax doesn't execute "error" statement but "success" again...

why?

thanks advance

M.

解决方案

Because the request didn't generate an error, where "error" means the server returns an HTTP error code. The server response may indicate an error in a different way, e.g., an error property.

You can check the response code and contents using Firebug or something similar; if you're expecting something other than what it sends back, add some details to the question stating what it returns, and what you'd like to see instead.

The easiest option is to set the statusCode or errorCode of the result as detailed in the JSON plugin docs. (Assuming that's the JSON plugin you're using.)

这篇关于struts2的Ajax错误结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆