ajaxSubmit错误捕获

时间:2012-11-27 19:06:47

标签: php ajax

我无法获取ajaxSubmit来捕获错误:

$(document).ready(function(){

        $("#supportForm").validate({
              rules: {
                 //validation
              },

              messages: {              
                 //messages
              },

              submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        url:"ajax/supportform",
                        type:"GET",
                        dataType: 'json',
                        error: function() { alert('error'); },
                        success: function() {alert('success'); },


                    });
              }

        });  
    })

我需要在php脚本中返回什么才能触发错误事件?

我试过返回一个错误=> 0的数组,退出(json_encode('error'=>'0');等等。

3 个答案:

答案 0 :(得分:4)

参见Mike de Klerk的链接,它提供了捕获错误必须采取的措施的线索:

看起来成功&错误回调与传递布尔错误/成功消息无关,但仅限于成功调用url的情况。我希望我没有得到错误结果,因为我使用的CMS总是返回某种内容 - 即使它是404或403页。

无论如何,我必须从我的php脚本中返回一个json字符串:

<?php


$response = json_encode(array("error" => "false","message" => "this is the error message"));

return $response;

然后在我的成功回调中解析它:

submitHandler: function(form) {
        $(form).ajaxSubmit({
            url:"ajax/supportform",
            type:"POST",
            dataType: 'json',

            error: function(data ) { alert(data); },

            success: function(response) {

            var obj = jQuery.parseJSON(response);

            if(obj.error == "true"){
                alert('error is true');
            }else{
                alert('error is false')
            }

            },


        });
  }

答案 1 :(得分:2)

编辑:要显示来自PHP的一些特定错误说明,您应该阅读以下问题:jQuery Ajax error handling, show custom exception messages

让您的服务器生成一些错误,使用PHP,您可以像这样做

<?php
header("HTTP/1.0 404 Not Found");
?>

查看标题功能的文档:http://nl.php.net/manual/en/function.header.php

你在这里做的是为你的webbrowser提供不属于典型HTML源代码的数据,webbrowser通过它知道出了什么问题。

您实际上可以将所需的所有元数据添加到网页请求的标题中,但是当Web浏览器不知道它意味着什么时,它就会被忽略。请在此处查看定义或状态代码:http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

答案 2 :(得分:0)

将url更改为不存在的强制错误的内容

          function(form) {
                $(form).ajaxSubmit({
                    url:"ajax/supportform001",
                    type:"GET",
                    dataType: 'json',
                    error: function() { alert('error'); },
                    success: function() {alert('success');} // <-was an extra comma
                });
          }
相关问题