dongxi7609
2014-06-06 06:02
浏览 51

Ajax从PHP函数中获取错误或成功消息

I'm post data to a php function using JSON post. I want to receive two different type of responses, success and error. For example, the following function:

function some_function(){

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

}

In this case, both messages will be returned as success. How can I make the error message return to the error function in the ajax? Here's the ajax code:

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);
    }

});

图片转代码服务由CSDN问答提供 功能建议

我使用JSON帖子将数据发布到php函数。 我希望收到两种不同类型的回复,成功错误。 例如,以下函数:

   function some_function(){
 
 if($ error){
 echo“some error”; 
 exit; 
} else {
 echo“一切正常”
退出; 
} 
 
 \  n} 
   
 
 

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

  jQuery.ajax({
 
 url:'&lt;  ?php echo $ url;?&gt;',
 data:response,
 dataType:'JSON',
 type:'POST',
 success:function(data){
 console.log(data)  ; 
},
错误:function(data,errorThrown){
 console.log(errorThrown); 
} 
 
}); 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • douewei1665 2014-06-06 06:11
    已采纳

    Try this one:

    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);
        }
    
    });
    

    On PHP page:

    function some_function(){
    
            if ($error){
                $msg = array("error"=>"some error";
            } else {    
                $msg = array("success"=>"everything is ok");
            }
           echo json_encode($msg);die;
        }
    
    已采纳该答案
    打赏 评论
  • dongshan4316 2014-06-06 06:27

    If you want to force it to go to the error callback, then just force a wrong response from the server. Consider this example:

    <?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>
    
    打赏 评论
  • douweibeng5219 2014-06-08 22:31

    Thanks.The below JSON example is working fine.pls find it.

    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;
     }
     }
    

    On the php script page

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

    Elangovan

    打赏 评论
  • douxian9010 2017-07-07 16:38

    I don't have enough reputation to comment, but as of JQuery 1.5 you'll need to use

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

    to get the json_encoded array your PHP is returning.

    打赏 评论

相关推荐 更多相似问题