jquery表单插件,无错误处理
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;
});
It seems that there are no error handling facility in the Jquery.Form plugin, which is very frustrating. Even though the documentation says we can use the $.ajax options, I still cannot make use of the 'error' option when the server returns an error, especially the 500 and 400 series. Is it that this plugin cannot handle any error at all from the server or is it a bug, etc?
Can someone please tell me how I can handle errors (400, 500, etc) with this plugin? I need your help... All I want is a simple error handling... Thank you.
$("#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;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
jQuery 表单插件确实处理错误,文档< /a> 正确地指出它可以使用 $.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!}”,使代码更干净、更容易理解。这是一个代码示例:
不良意愿响应将在控制台日志中打印此内容:
我不知道为什么“完整”回调运行两次 - 有人吗?
最后一件事,如果您想知道为什么 jQuery 或 Form 插件可以,请阅读此线程不只是读取 iframe 的 HTTP 标头。
The jQuery Form Plugin does handle errors and the documentation is correct in stating it can use the $.ajax options. However there are two modes the plugin will run in - normal and file upload. The problem you are having is because you are performing a file upload. (You don't say this but you have "#uploadingImg" and "#polygonUploadForm" and, recursive reasoning, you are having this problem.)
This is commonly stated, but you can't upload a file with AJAX. This workaround is to use an iframe. Form submit POSTs a normal HTTP request and loads the server response in the iframe. The plugin grabs the response and voila. Because this happens as a normal HTTP request, the rules and behaviors of $.ajax don't apply (because it isn't being used). The only thing Form plugin can do is treat whatever it gets as a success. In code terms, file uploading will never trigger the 'error' attribute assigned to ajaxForm() or ajaxSubmit(). If you really want to prove this isn't an AJAX request, attach an ajaxComplete() handler to the form (i.e. completely independent of the Form plugin's methods). It won't get triggered either. Or look at Firebug's Net->XHR panel.
So that's that - the upload is not an AJAX request. The best you can do is work within the 'success' or 'complete' callbacks of ajaxForm()/ajaxSubmit(). You are limited without access to the actual HTTP response code, but you can examine the response. If the response is empty or not what you expect, you can actually trigger the 'error' callback by calling
xhr.abort()
. Instead of doing "if(good response) {yay!} else {oh no!}", calling abort() and triggering 'error' makes the code a little cleaner and more understandable IMHO. Here's a code example:A bad will response will print this in the console log:
I don't know why the 'complete' callback is run twice - anyone?
One final thing, read this thread if you are asking why jQuery or the Form plugin can't just read the HTTP headers of the iframe.
刚刚发现 jquery.form 插件本身不处理服务器错误,因为由于“文件输入”标记处理文件上传的方式,它无法访问响应标头。因此,我认为 W3C 需要审查这个麻烦的标签,它不仅不允许您使用 CSS 对其进行样式设置,而且也不会使服务器响应处理变得容易。
不过,一定有办法解决这个问题...
如果我说错了什么,请告诉我,并让我知道您对这个“文件输入”标签的想法...
Just found out that the jquery.form plugin itself doesn't handle server error, because it doesn't have access to response headers due to the way 'file input' tag handles file upload. So I think the W3C needs to review that troublesome tag that not only doesn't let you style it with CSS but also doesn't make server response handling easy.
There must be a way around it though...
Let me know if I said something wrong and let me know your thoughts about this 'file input' tag...
Jquery Form Plugin处理服务器响应状态成功(代码200),我认为这些信息已经足够了。
从那里你可以创建你的响应状态服务器端
当然 returnJson 是一个函数,
用于在成功句柄的前端发送 Json 数据(你可以构建一个或在你的方法中执行)(当响应为 200 时触发)你只需要检查您的状态字段(在本例中为“响应”),示例如下:
Jquery Form Plugin handle the server response status success (code 200), i think that information is enough.
From that you can create you response status server side
of course returnJson is a function to send the Json data (you can build one or doing inside your method)
in the frontend on success handle (is fired when response is 200) you just need to check your status field (in this case 'response'), example below:
在选项中,
发生错误时不会触发调用。当您收到 500 或 404 时,该插件就会挂起。
成功只是对“成功”采取行动,所以这就是我们现在所拥有的一切。
In options
there is no trigger call in case of an error. The plugin just hangs when you get a 500 or 404.
The success is only acting on 'success' so that's all we have for now.
对于任何仍然需要这个的人来说,这是让它发挥作用的一种方法
For anybody still needing this, this is a way of getting it to work