July 5, 2013
$.ajax: Unable to produce the error if data is 0
User1739825’s Question:
Please advise as I am new to jQuery.
The following code is:
function get(database)
{
var dbValue = database;
console.debug("dbvalue read as :", dbValue)
$.ajax// use of jQuery here
({
type: "POST",
url: "testconnect.php",
data: {nameDB: dbValue},
success: function(data)
{
alert("Successfully login and connected");
}
error: function(data)
{
alert("Unsuccessfully login and connected");
}
});
return false;
}
If the data is is 1(true), alert will show “Successfully login and connected”. But what if the error is 0, it cannot produce “Unsuccessfully login and connected”.
error: function() {}
will trigger if there is an error in the AJAX request. If the AJAX request does not succeed then only this function will execute.
You can do what you are trying to by comparing the output on your success function, like this:
success: function(data) {
if(data == '1') alert("Successfully login and connected");
else alert("Unsuccessfully login and connected");
}