Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger the "error" callback in JQuery AJAX call using in PHP

I have PHP code snippet the following:

if (!($result = mysql_query($query, $link))) {
   die("Invalid SQL query: " . $query);
}

And I have JQuery code snippet the following:

$.ajax({
        url: "....search.php", 
        data: ...,

        async: false, //to trigger error alert

        success: function(xml) {
            ...
        },

        error: function(xml) {
            foundError = true;
        },

        dataType: "xml"
    });

if(foundError) {
        setProgress("Could not complete the search because an error was found", ProgressBar.ERROR);
}

Is it possible to have the die call trigger JQuery error function callback? If not, how would I trigger it otherwise?

like image 746
Daniel Kats Avatar asked Jul 05 '11 21:07

Daniel Kats


People also ask

What triggers jQuery ajax error?

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the .ajaxError() method are executed at this time. Note: This handler is not called for cross-domain script and cross-domain JSONP requests.

How do I force ajax call error?

If you want to manually trigger the error callback based on the value of the received data you can do so quite simple. Just change the anonymous callback for error to named function. @dallin because it allows you to not duplicate code. The server might respond with code 200 which triggers the "success" callback.

How do you trigger a change event after ajax call?

ajax({ url, data: data, type: "POST", dataType: "json", success: function(cbdata) { update_table(cbdata); } }); } $('#selector'). on('click', changeDate); So you can call changeData() when you need it.

What is callback jQuery ajax?

jQuery - ajaxSuccess( callback ) Method The ajaxSuccess( callback ) method attaches a function to be executed whenever an AJAX request completes successfully. This is an Ajax Event.


1 Answers

Try this:

if (!($result = mysql_query($query, $link))) {
   header("HTTP/1.1 404 Not Found");
}

Choose the error appropriate for your application

like image 64
AlienWebguy Avatar answered Nov 14 '22 22:11

AlienWebguy