I have a form in html, I'll do the validation on the server side. If the validation fails, the errorDiv will be updated, otherwise the resultDiv will be updated.
I have two options to handle this. My friend said the validation failure should handle in the on success part of ajax call. While I think put the logic in the error part might be better.
Some of his concerns might be:
- The validation error is not some error like network connection failure which should use some status code like 400; or any server side error, which using the status code 500.
My concern is:
- If we put the logic in the error part, the logic seems more clear;
- we can use some other status code (although I don't know right now) rather than 400, 500 to indicate the validation error.
My friend's Option 1:
$.ajax({
type:'POST',
data:someFormData,
url:'/submitToServer',
success:function(data,textStatus) {
if(data.hasValidationError){
$('#errorDiv').html(data);
}else{
$('#resultDiv').html(data);
}
}
});
My Option 2:
$.ajax({
type:'POST',
data:someFormData,
url:'/submitToServer',
success:function(data,textStatus) {
$('#resultDiv').html(data);
},
error:function(XMLHttpRequest,textStatus,errorThrown){
if(textStatus==500)
$('#errorDiv').html(errorThrown);
}
});
I do know both ways can handle the validation errors, but which way is better? Which way are you using? Please give me some pros and cons...
Thank you guys so much!