AJAX / JQuery success: / error: function manipulation

Some problems with AJAX / JQuery. Here is the context of my problem, followed by a sample code:

What I'm trying to do is call a PHP script called getInfo.php and check if any data is contained in the database. I can write queries quite easily, but from the point of view of the code example below, how can I “say” that the success function fails if it cannot find the data in the database and instead run the error function?

$(document).ready(function(){
        getInfo();
        function getInfo(){
            $.ajax({
                type: "GET",
                url: "getInfo.php",
                data: "do=getInfo",
                cache: false,
                async: false,
                success: function(result) {
                    $("#myInfo").remove();
                    alert("Data found");
                },
                error: function(result) {
                    alert("Data not found");
                }
            });
        }
});

Any advice would be greatly appreciated. =)

+3
source share
4 answers

An error handler is used to handle errors in your AJAX call.

1 PHP , , 0, . if, , . :

success: function(result)
{
    if(result == 1)
    {                    
        $("#myInfo").remove();
        alert("Data found");
    }
    else
    {
        alert("Data not found");
    }
},
+5

"success" , "200" ( ). "error" , (, 404, 500).

, , 2 :

  • PHP 404,
  • getinfo.php ( )

    { "success": true,...}

+3

"" AJAX, script. script, , (.. !), .

It is best to have your getInfo.php script return what you can use in the success function; for example, the number of rows in a result set or something else - then you can check success () if you have data and code, respectively.

+2
source

I think your getInfo.php page should just print SUCCESS or FIX, and in your success method do

success: function(result) {
        if (result == 'SUCCESS')
        {
          $("#myInfo").remove();
          alert("Data found");
        }
        else
        {
          alert("Data not found");
        }
    }
0
source

All Articles