使用jQuery AJAX等待JSON响应时处理PHP错误

时间:2016-06-14 15:44:45

标签: php jquery json ajax

我必须通过jQuery AJAX发送表单,检索JSON响应并检查例如data.name和data.email是否“成功”以使用户知道他的表单已提交。

但是当例如存在PHP错误并且没有JSON响应时。什么是最好的方法,因为回调消息将永远是“发送...”,而有一个响应告诉PHP有错误。

1 个答案:

答案 0 :(得分:1)

JavaScript的:

<script>
$(document).ready(function(){
   $.ajax({
      url : "yourpage.php",
      data : {your_data},
      dataType : "json",
      type : "POST",
      success : function(response){
         //Here goes success part of your program.
      },
      error : function(a,b,c){
         console.log(a+" "+b+" "+c);
         //Your error shows here.
      }
   });
});
</script>