【问题标题】:Ajax get the error or success message from PHP functionAjax 从 PHP 函数中获取错误或成功消息
【发布时间】:2014-07-27 07:59:08
【问题描述】:

我正在使用 JSON post 将数据发布到 php 函数。我想收到两种不同类型的响应,成功错误。 例如下面的函数:

function some_function(){

    if ($error){
        echo "some error";
        exit;
    } else {    
        echo "everything is ok"
        exit;
    }

}

在这种情况下,两条消息都将作为成功返回。如何使错误消息返回到ajax中的错误函数? 这是ajax代码:

jQuery.ajax({

    url: '<?php echo $url; ?>',
    data: response,
    dataType: 'JSON',
    type: 'POST',
    success:function(data){
        console.log(data);
    },
    error: function(data, errorThrown){
        console.log(errorThrown);
    }

});

【问题讨论】:

  • 当来自服务器的响应不是你期望的那样时,将执行错误回调。在你的情况下,你期待 Json。但是,如果响应不是 json 格式,则会转到错误函数

标签: php jquery ajax


【解决方案1】:

试试这个:

jQuery.ajax({

    url: '<?php echo $url; ?>',
    data: response,
    dataType: 'JSON',
    type: 'POST',
    success:function(data){
        if(data['error']){
           alert(data['error']);
          }else{
             alert(data['success']);
          }
    },
    error: function(data, errorThrown){
        console.log(errorThrown);
    }

});

在 PHP 页面上:

function some_function(){

        if ($error){
            $msg = array("error"=>"some error";
        } else {    
            $msg = array("success"=>"everything is ok");
        }
       echo json_encode($msg);die;
    }

【讨论】:

  • 我在控制台日志中得到Undefined :(
【解决方案2】:

如果你想强制它去错误回调,那么只需强制服务器做出错误的响应。考虑这个例子:

<?php

$url = 'index.php'; // sample value

function response($value) {
    if($value == 'true') {
        http_response_code(200);
        $data = array('status' => 200, 'message' => 'Server response is okay!');
        echo json_encode($data);
    } else {
        http_response_code(404);
    }
    exit;
}

if(isset($_POST['response'])) {
    $response = $_POST['response'];
    response($response);
}

?>


<script src="jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    jQuery.ajax({

        url: '<?php echo $url; ?>',
        data: {response: true}, // <-- set to TRUE or FALSE 
        dataType: 'JSON',
        type: 'POST',
        success:function(data){
            console.log(data);
        },
        error: function(data, errorThrown){
            console.log('error => ' + errorThrown);
        }

    });

});
</script>

【讨论】:

    【解决方案3】:

    谢谢。下面的 JSON 示例运行良好。请找到它。

    jQuery.ajax({
    
    url: '<?php echo $url; ?>',
    data: response,
    dataType: 'JSON',
    type: 'POST',
    success: response_function
    });
    
    response_function(response){
     if(response.status == 1){
         //success response
         alert(response.message);
     }else{
          //failure response 
          alert(response.message);
          return false;
     }
     }
    

    在php脚本页面上

     function some_function(){
    
        if ($error){
            $msg = array("status"=> '1',    "message"=>"success message");
        } else {    
            array("status"=> '0', "message"=>"some error");
        }
       echo json_encode($msg);die;
    }
    

    埃朗戈文

    【讨论】:

      【解决方案4】:

      我没有足够的声誉来发表评论,但从 JQuery 1.5 开始,您需要使用

      error: function(data) {
          console.log(data['responseText']);
      }
      

      获取 PHP 返回的 json_encoded 数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-18
        • 1970-01-01
        • 1970-01-01
        • 2020-01-19
        • 1970-01-01
        • 2023-04-06
        • 2017-11-11
        • 2017-07-26
        相关资源
        最近更新 更多