Ging3r Ging3r - 1 year ago 159
jQuery Question

Ajax error result with struts2

I have this configuration in struts.xml

<action name="updateAction"
<result name="success" type="json"/>
<result name="error" type="json"/>

this is my Action class

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

at last, this is jQuery ajax function

url: 'updateAction.action',
data : {
'actionId': id,
'actionName': name,
success: function(data) {

error: function(data){

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


thanks advance


Answer Source

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