Reputation: 23
I want to show the error message i get from controller in ajax error. So in my ajax error i have:
error: Function(e){
Errordiv.empty();
Errordiv.show();
Errordiv.append(e.responseText);
$.each(e.responseJSON.fieldErrors, function(i, v) {
Errordiv.append(v.message);
Errordiv.append("<br>");
});
}
The problem is i get both of them. I don't want this. I want if there is any field error, no other message be displayed and if there is not then the error that controller sends be shown. I put a condition if
if(e.responseJSON.fieldErrors==null){...}
Then show other message and so on but it didn't work. How to solve it?
NOTE: Example message come from controller is "you have voted before!".
Update: fieldErrors are the validation messages of entity class. If the fields are not valid then the errors will be shown in Errordiv. And if all fields are ok but the user has voted before, the controller will send a message that will be shown by "Errordiv.append(e.responseText)". Now when there is a field error, the erros are shown twice. Once by the foreach loop and once by "Errordiv.append(e.responseText)". But when fields are valid it is ok and the errordiv contains only the message.
Upvotes: 0
Views: 395
Reputation: 23
I solved it by changing the code to this:
Errordiv.empty();
Errordiv.show();
Errordiv.append(e.responseText);
If(e.responseJSON.fieldErrors.length>0){
Errordiv.empty();
$.each(e.responseJSON.fieldErrors, function(i, v) {
Errordiv.append(v.message);
Errordiv.append("<br>");
});
}
}
});
});
Upvotes: 1