简体   繁体   English

$ .ajax从PHP文件获取返回值

[英]$.ajax get return value from a PHP file

here I have jQuery ajax calling a .php file that SOMETIMES executes let's say the following 在这里,我有jQuery ajax调用SOMETIMES执行的.php文件,下面说

echo "hello"

Here it is: 这里是:

    $.ajax({
        type: "POST",
        url: myurl.php
        data: data_string,
        timeout: 6000,
        success: function () {

        }
    });

I would like to know: is it possible to make the ajax return ERROR and not SUCCESS when something like the previous echo is executed in the PHP file? 我想知道:在PHP文件中执行类似先前回显的操作时,是否有可能使Ajax返回ERROR而不是SUCCESS? I mean, checking inside this $.ajax if the php file is executed as I would or not. 我的意思是,检查$ .ajax内部是否按照我的意愿执行了php文件。

EXPLAINING BETTER: I get error when the request could not be completed and success when it could. 更好地说明: 当请求无法完成时,我会报错,而在请求可以成功时,我会报错。 But I would like to get like a return value from this PHP file. 但我想从此PHP文件中获得返回值。 If it returns 1, I wanna do something. 如果返回1,我想做点什么。 If it returns 2, instead, I wanna do something else. 如果返回2,我想做其他事情。 Hope I explained it better.. 希望我能更好地解释。

Thanks in advance. 提前致谢。

I recommend using json_encode() within your PHP file. 我建议在您的PHP文件中使用json_encode() For example: 例如:

echo json_encode(array('success' => 'do_foo'));
exit();

Then you can add a conditional within the success callback: 然后,您可以在成功回调中添加条件:

$.ajax({
  type: "POST",
  url: myurl.php
  data: data_string,
  dataType: "JSON", //tell jQuery to expect JSON encoded response
  timeout: 6000,
  success: function (response) {
    if (response.success === 'hello'){
      console.log(response);
    } else {
      console.log('else');
    }
  }
});

Based on your question make php return 1 or return 2. You can make it return 1 on failure and 0 (which is null) on success. 根据您的问题,使php返回1或返回2。您可以使其在失败时返回1,在成功时返回0(为null)。 Then you can do this for your ajax return. 然后,您可以这样做以获取ajax返回。

$.ajax({
                type: "POST",
                url: "YOUR URL",
                data: dataString,
                success: function(server_response)
                    {
                        if(server_response == 1)
                        {
                            alert("You have made a mistake");
                            return true;
                        }

                        HERE YOU WILL PUT WHAT HAPPENS ON SUCCESS

                        }
                    });
$.ajax({
    type: "POST",
    url: myurl.php
    data: data_string,
    timeout: 6000,
    success: function (msg) {
       if (msg != "Hi")
       {
          //TODO
       } else
       {
          //TODO
       }
    }
});

You can use error callback for this: 您可以为此使用错误回调:

$.ajax({
    type: "POST",
    url: myurl.php
    data: data_string,
    timeout: 6000,
    success: function () {

    },
    error: function() {
        /* Code reacting to error here */
    }
});

Also, there are other callback opportunities which you can check out at $.ajax documentation page . 此外,还有其他回调机会,您可以在$ .ajax文档页面上查看

If you are going to "say" fron PHP that there is an error, this can be done with 2 ways: 如果您要“说” PHP程序有错误,可以通过以下两种方法完成:

You can print in PHP a keyword meaning an error and then check it in you success function: 您可以在PHP中打印一个表示错误的关键字,然后在成功函数中进行检查:

success: function (data) {
    if (data == 'error') {
    } else {
    }
}

Or, the other way, you can provide with PHP right headers to cause "error". 或者,通过另一种方式,您可以提供PHP正确的标头来引起“错误”。 Then you can use the error callback as usual. 然后,您可以照常使用错误回调。 I would choose this way. 我会选择这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM