Javascript AJAX/JQuery 成功:/错误:函数操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5376896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:56:00  来源:igfitidea点击:

AJAX/JQuery success:/error: function manipulation

phpjavascriptjqueryajax

提问by Riyan

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

使用 AJAX/JQuery 时遇到一些麻烦。这是我的问题的上下文,然后是代码示例:

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?

我试图做的是调用一个名为 getInfo.php 的 PHP 脚本并检查某些数据是否包含在数据库中。我可以很容易地编写查询,但就下面的代码示例而言,如果无法在数据库中找到数据并运行错误函数,我如何“告诉”成功函数失败?

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

任何建议将不胜感激。=)

回答by Michiel Pater

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

错误处理程序用于处理 AJAX 调用中的错误。

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

1如果找到了数据,0如果没有找到,您可以在 PHP 脚本中回显。然后您可以使用 if 语句来确定要做什么。例如:

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

回答by Bart Vangeneugden

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

当返回的代码为“200”(成功请求)时,会调用“success”。每当返回另一个代码(例如 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, ...}

  • 让 PHP 返回 404,以便调用错误函数
  • 让你的 getinfo.php 返回一个布尔值(通常是我的方法)

    {“成功”:真,...}

回答by Codecraft

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.

您使用的“错误”函数用于识别和处理 AJAX 错误,而不是脚本错误。如果找到了您正在调用的脚本,并且它在执行时没有意外终止(即,它有错误!),那么它被认为是成功的。

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.

最好的办法是让你的 getInfo.php 脚本返回一些你可以在成功函数中使用的东西;例如结果集中的行数之类的 - 然后您可以检查 success() 是否有相应的数据和代码。

回答by Gabriele Petrioli

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

我认为你的 getInfo.php 页面应该只打印 SUCCESS 或 FAIL 并且在你的成功方法中做

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