jquery-plugins - jquery表单插件,无错误处理

标签 jquery-plugins jquery file-upload jquery-forms-plugin ajax-upload

Jquery.Form 插件中似乎没有错误处理功能,这非常令人沮丧。尽管文档说我们可以使用 $.ajax 选项,但当服务器返回错误时,我仍然无法使用 'error' 选项,尤其是 500 和 400 系列。这个插件是否根本无法处理来自服务器的任何错误,或者是一个错误等? 有人可以告诉我如何使用此插件处理错误(400、500 等)吗?我需要你的帮助...我想要的只是一个简单的错误处理...谢谢。

$("#uploadingImg").hide();

var options = {//Define ajax options
    type: "post",
    target: "#responsePanel",
    beforeSend: function(){
        $("#uploadingImg").show();
    },
    complete: function(xhr, textStatus){
        $("#uploadingImg").hide();
    },
    success: function(response, statusString, xhr, $form){
        // I know what to do here since this option works fine
    },
    error: function(response, status, err){
        // This option doesn't catch any of the error below, 
        // everything is always 'OK' a.k.a 200
        if(response.status == 400){
            console.log("Sorry, this is bad request!");
        }
        if(response.status == 601){
            sessionTimedOut();
        }
    }
}
$("#polygonUploadForm").submit(function(){
    $(this).ajaxSubmit(options); // Using the jquery.form plugin
    return false;
});

最佳答案

jQuery 表单插件确实处理错误,并且 documentation说明它可以使用 $.ajax 选项是正确的。但是,该插件有两种运行模式 - 正常模式和文件上传模式。您遇到的问题是因为您正在执行文件上传。 (你没有这么说,但你有“#uploadingImg”和“#polygonUploadForm”,并且递归推理,你遇到了这个问题。)

这是常见的说法,但您无法使用 AJAX 上传文件。此解决方法是使用 iframe。表单提交 POST 一个普通的 HTTP 请求并在 iframe 中加载服务器响应。该插件会获取响应,瞧。 因为这是一个普通的 HTTP 请求,所以 $.ajax 的规则和行为不适用(因为它没有被使用)。表单插件唯一能做的就是将其获得的任何结果视为成功。用代码术语来说,文件上传永远不会触发分配给 ajaxForm() 或 ajaxSubmit() 的“error”属性。如果您确实想证明这不是 AJAX 请求,请将 ajaxComplete() 处理程序附加到表单(即完全独立于表单插件的方法)。也不会被触发。或者查看 Firebug 的 Net->XHR 面板。

就是这样 - 上传不是 AJAX 请求。您能做的最好的事情就是在 ajaxForm()/ajaxSubmit() 的“成功”或“完成”回调中工作。您无法访问实际的 HTTP 响应代码,但您可以检查响应。如果响应为空或不是您所期望的,您实际上可以通过调用 xhr.abort() 来触发“错误”回调。恕我直言,调用 abort() 并触发“错误”,而不是执行“if(good response) {yay!} else {oh no!}”,使代码更干净、更容易理解。这是一个代码示例:

$("#uploadForm").ajaxForm({
    success: function(response, textStatus, xhr, form) {
        console.log("in ajaxForm success");

        if (!response.length || response != 'good') {
            console.log("bad or empty response");
            return xhr.abort();
        }
        console.log("All good. Do stuff");
    },
    error: function(xhr, textStatus, errorThrown) {
        console.log("in ajaxForm error");
    },
    complete: function(xhr, textStatus) {
        console.log("in ajaxForm complete");
    }
});

不良意愿响应将在控制台日志中打印此内容:

[jquery.form] state = uninitialized
"NetworkError: 404 NOT FOUND - http://server/fileupload/"
[jquery.form] state = loading
[jquery.form] isXml=false
in ajaxForm success
bad or empty response
[jquery.form] aborting upload... aborted
in ajaxForm error
in ajaxForm complete
in ajaxForm complete

我不知道为什么“完整”回调运行两次 - 有人吗? 最后一件事,请阅读 this thread如果您问为什么 jQuery 或 Form 插件不能只读取 iframe 的 HTTP header 。

关于jquery-plugins - jquery表单插件,无错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3995355/

相关文章:

jquery - 我可以在 JQuerify 等书签中使用 JQuery 插件吗

javascript - 在 javascript 中输入带有事件的文本字段并在目标元素中自动填充值

javascript - 混合 QooXDoo 和 jqgrid

jquery - 逗号分隔的自动完成与 jquery 自动完成

javascript - 想要删除空行 Ace Editor

javascript - 从输入字段中获取值,计算并显示总计

android - 使用 ssl 的 phonegap 中的文件上传问题

php - 如何在 SonataAdminBundle 的上传字段上方显示当前图片?

java - 多文件上传只上传最后一个文件而不上传其余文件

jquery - 为什么 jQuery 插件中是 this 而不是 $(this)