$.ajax({
type: "POST",
url: "/zzz/bbb/abcd.aspx?deleteIDs=" + request_ids,
contentType: "application/x-www-form-urlencoded",
data: '',
dataType: "html",
complete: function(xhr, status) {
if (status == "success")
{
}
else {
}
}
The jquery code above has its status =”success” regardless of the outcome of the background operation. What am I doing wrong?
“Success” only refers to the response from the server. If the server response is a 200 response, it will be considered a success, no matter if the business logic on the server side encountered an issue.
You can either return a 400 or 500 error code from the server, or preferably, you send a response back to the JS that contains any error messages.
For example, I usually expect a JSON response from the server. If there was an error on the server side, it contains an error:true flag and I handle it in the JS. So a typical errorless response would be:
An error on the server would return:
EDITed to give an example:
Expecting a JSON response as outlined above, I could do something like this: