Ajax Success and Error function failure
Solution 1:
Try this:
$.ajax({
beforeSend: function() { textreplace(description); },
type: "POST",
url: "updatedjob.php",
data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status,
success: function(){
$("form#updatejob").hide(function(){$("div.success").fadeIn();});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
The beforeSend
property is set to function() { textreplace(description); }
instead of textreplace(description)
. The beforeSend
property needs a function.
Solution 2:
One also may use the following to catch the errors:
$.ajax({
url: url,
success: function (data) {
// Handle success here
$('#editor-content-container').html(data);
$('#editor-container').modal('show');
},
cache: false
}).fail(function (jqXHR, textStatus, error) {
// Handle error here
$('#editor-content-container').html(jqXHR.responseText);
$('#editor-container').modal('show');
});
Solution 3:
I was having the same issue and fixed it by simply adding a dataType = "text" line to my ajax call. Make the dataType match the response you expect to get back from the server (your "insert successful" or "something went wrong" error message).
Solution 4:
You can implement error-specific logic as follows:
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (textStatus == 'Unauthorized') {
alert('custom message. Error: ' + errorThrown);
} else {
alert('custom message. Error: ' + errorThrown);
}
}