最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - AJAXJQuery success:error: function manipulation - Stack Overflow

programmeradmin3浏览0评论

Having some trouble with AJAX/JQuery. Here is the context of my problem followed by a code sample:

What I am attempting to do is call a PHP script called getInfo.php and check whether or not some data is contained within a database. I can write queries easily enough but in terms of the code sample below how can I "tell" the success function to fail if it cannot find data in the database and run the error function instead?

$(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. =)

Having some trouble with AJAX/JQuery. Here is the context of my problem followed by a code sample:

What I am attempting to do is call a PHP script called getInfo.php and check whether or not some data is contained within a database. I can write queries easily enough but in terms of the code sample below how can I "tell" the success function to fail if it cannot find data in the database and run the error function instead?

$(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. =)

Share Improve this question asked Mar 21, 2011 at 11:18 RiyanRiyan 1872 gold badges5 silver badges9 bronze badges 1
  • you can have the php side output out an error message and within your success function check to see if the result matches the error, and if it does you alert('data not found'); – KJYe.Name Commented Mar 21, 2011 at 11:21
Add a comment  | 

4 Answers 4

Reset to default 7

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

You could echo 1 in your PHP script if the data was found and 0 if it was not found. Then you could use an if statement to determine what to do. For example:

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

"success" gets called when the returned code is a "200" (successfull request). "error" gets called whenever another code is returned (e.g. 404, 500).

So I think you can do 2 things:

  • Let PHP return a 404 so the error function gets called
  • Let your getinfo.php return a boolean value (usually my approach)

    {"success":true, ...}

The 'error' function you're using is for identifying and handling an AJAX error, not a script error. If the script you're calling is found, and it executes without terminating unexpectedly (ie, it has errors!), then its considered a success.

The best thing to do is have your getInfo.php script return something you can use within the success function; such as the number of rows in your result set or something - then you can check in success() whether you have data and code accordingly.

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

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

评论列表(0)

  1. 暂无评论