Ging3r Ging3r - 5 months ago 54
jQuery Question

Ajax error result with struts2

I have this configuration in struts.xml

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


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.

Answer

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.)