使用 $('.ajax_form').ajaxForm(); 的奇怪行为
这是我第一次在像下面这样的类上应用 jquery ajaxForm
<form class="ajax_form"...><input type="text" name="q" /><input type="submit" /></form>
<form class="ajax_form"...><input type="text" name="q" /><input type="submit" /></form>
<script>
$('.ajax_form').ajaxForm({
dataType: 'json',
error: ajaxErrorHandler,
success: function(response) { // do some ui update .. }
});
</script>
现在,在 Ajax 调用完成后,我总是进入错误部分,尽管 firebug 没有报告任何错误响应,但不确定我做错了什么。
It is my first time to apply jquery ajaxForm on a class like the following
<form class="ajax_form"...><input type="text" name="q" /><input type="submit" /></form>
<form class="ajax_form"...><input type="text" name="q" /><input type="submit" /></form>
<script>
$('.ajax_form').ajaxForm({
dataType: 'json',
error: ajaxErrorHandler,
success: function(response) { // do some ui update .. }
});
</script>
Now after Ajax call is completed I always get into error section although firebug didn't report any errors response not sure what I did wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在这种情况下,这可能合适,也可能不合适,但我会提供它,因为当我寻找类似问题的答案时,它对我很有用。 如果您在 Firefox 中提交带有文件上传功能的“multipart/form-data”表单,jquery.form 将使用 iframe 提交表单。 如果返回数据的 Content-Type 是 text/plain,则 iframe 会将结果文本包装在
在我弄清楚之前,这给我带来了无尽的头痛(在这个帖子的帮助下:http://www.extjs .com/forum/archive/index.php/t-17248.html)。
我的例子的答案是确保响应内容类型是“text/html”(这是违反直觉的,至少对我来说)。
This may or not be appropriate in this case, but I'll provide it because it would have been useful to me when I was searching for the answer to a similar problem. If you are submitting a "multipart/form-data" form with file upload in Firefox, jquery.form will use an iframe to submit the form. If the Content-Type of your return data is text/plain, the iframe will wrap the resulting text in <pre> tags which will hork the jquery json parser and give you a parser error even though Firebug shows the response and even the json correctly.
This caused me no end of headaches before I figured it out (with help from this thread: http://www.extjs.com/forum/archive/index.php/t-17248.html).
The answer in my case was to make sure the response Content-Type was "text/html" (which was counter-intuitive, at least for me).
@c.sokun:这里使用类不应该成为问题,只要只有 1 个表单使用该类。 同一页面上具有相同类的两个表单肯定会导致出现问题(参考您的代码...或者是拼写错误?)
您是否尝试过使用 FireBug 并检查传递的参数和返回的值? 那应该是第一个!
@c.sokun: Using a class shouldn't be a problem here, as long as there's only 1 form using the class. Two forms with the same class on the same page will definitely cause a hiccup (refer to your code... or is it a typo?)
Have you tried using FireBug and checked the parameters passed and the values returned? That should be the first!
问题出在你的 json 数据上。 这些可能格式不正确并且无法解析。 在这种情况下,不会调用 success 函数。
如果您打印错误回调的消息,您可以验证这一点。 使用以下代码:
打印输出之一应该类似于“解析器错误”。
The problem is with your json data. These are probably not well formed and can't be parsed. In that case the success function won't be called.
You can verify this if you print the messages of the error callback. Use the following code:
One of the print-outs should be something like "parser error".
我认为您需要表单的 url 和 post 类型才能将数据发送到某处?
这就是他们在 jquery.com 上的设置方式:
I think you would need a url and post type for the form to send the data somewhere?
this is how they set it up on jquery.com:
ajaxErrorHandler 是否在其他地方定义?
我尝试了你的代码,它运行得很好。 您使用什么版本的 jQuery 和 jQuery 表单?
这是我尝试过的代码。 与此测试位于同一目录中的包含“{test:'hello world'}”的名为“test.json”的文件:
Is ajaxErrorHandler defined elsewhere?
I tried your code, and it worked perfectly. What version of jQuery and jQuery form are you using?
This is the code I tried. With a file named 'test.json' containing "{test:'hello world'}" in the same directory as this test:
好吧,我检查了 API,没有找到任何对名为“错误”的选项字段的引用,所以可能就是这个原因。 检查此处。
Well, I've checked in API and didn't find any reference to options field called "error", so probably thats the reason. Check here.