marky
marky

Reputation: 5068

jQuery $.ajax - Calling error function from success function?

In a php script, after an attempt at a database insert, I'm returning to the calling $.ajax() function either the id of the new record or an error message:

if ($newID > 0) {
    // Successfully added new record
    echo $newID;
} else {
    // No new record was inserted
    echo 'Error: ' . mysqli_error($dbc);
}

My question is: if the php else clause gets fired and I'm passing a string via the echo, then won't the success() function be called in the $.ajax function? Is there a way to somehow "fire" the $.ajax error() function depending on what's passed from the php script?

Upvotes: 0

Views: 289

Answers (1)

Toddish
Toddish

Reputation: 516

$.ajax.error() shouldn't really be fired, as the AJAX call was a success. However, I guess you could return a different header if you really wanted to:

header("HTTP/1.0 404 Not Found");

See http://php.net/manual/en/function.header.php and http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Upvotes: 1

Related Questions