Reputation: 6289
Ajax error function not calling in java
Here is the ajax method:-
$.ajax({
type:"post",
timeout:5000,
url:"<s:property value="URL"/>",
data:{name:newName},
success:function () {
$("#errorDiv").html("Successfully updated");
},
error:function (data) {
$("#errorDiv").html("Error.");
}
})
Update User method:-
@Action(value = "renameUser", results = {@Result(name = "success", type = "json"),
@Result(name = "input", type = "tiles", location = "view.list")},
interceptorRefs = {@InterceptorRef("auditingAdminDefault")})
@Override
public String execute() throws Exception {
String result = "input";
try {
updateUser(name);
addActionMessage(name + " user name was successfully updated.");
result = "success";
} catch (Exception e) {
addActionMessage(e.getMessage());
result = "input";
}
return result;
}
}
updateUser();
updateUser() throws Exception {
if (..) {
} else {
throw Exception();
}
I am able to update user successfully without issues. But success function is called for successful update or any error thrown.
There are two things I need to use in Ajax.
Kindly anyone tell me what I am doing wrong here ?
Upvotes: 1
Views: 1239
Reputation: 6289
I am able to solve the following issues:-
Following is the updated method and its working now:-
Update User method:-
private String errorStatement;
public String getErrorStatement() {
return errorStatement;
}
@Action(value = "renameUser", results = {@Result(name = "success", type = "json"),
@Result(name = "input", type = "json", , params = { "statusCode", "500" })},
interceptorRefs = {@InterceptorRef("auditingAdminDefault")})
@Override
public String execute() throws Exception {
try {
updateUser(name);
return SUCCESS;
} catch (Exception e) {
errorStatement = e.getMessage();
return ERROR;
}
}
}
Upvotes: 0
Reputation: 9794
you are catching all the errors in your action method and returning a valid response to the client .
catch (Exception e) {
addActionMessage(e.getMessage());
result = "input";
}
return result;
This is why even in case of failure, the success call back is called since for client side the response is a valid one. you should return an error in the action method in case some exception occurs.
Ideally you should return an Error View for any exception.
catch (Exception ex)
{
addActionMessage(e.getMessage());
return View("Error");
}
but in your case you want to display the error in the same view hence you can do something like that
you can do something like this for exception.
public ActionResult execute() throws Exception {
{
try
{
//code everything works fine
}
catch (Exception ex)
{
return new HttpStatusCodeResult(500);
}
}
Upvotes: 1