使用 $('.ajax_form').ajaxForm(); 的奇怪行为

发布于 2024-07-21 10:55:59 字数 543 浏览 10 评论 0原文

这是我第一次在像下面这样的类上应用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只有影子陪我不离不弃 2024-07-28 10:55:59

在这种情况下,这可能合适,也可能不合适,但我会提供它,因为当我寻找类似问题的答案时,它对我很有用。 如果您在 Firefox 中提交带有文件上传功能的“multipart/form-data”表单,jquery.form 将使用 iframe 提交表单。 如果返回数据的 Content-Type 是 text/plain,则 iframe 会将结果文本包装在

 中。   即使 Firebug 正确显示响应甚至 json,它也会扰乱 jquery json 解析器并给你一个解析器错误。

在我弄清楚之前,这给我带来了无尽的头痛(在这个帖子的帮助下: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).

浅忆 2024-07-28 10:55:59

@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!

清风疏影 2024-07-28 10:55:59

问题出在你的 json 数据上。 这些可能格式不正确并且无法解析。 在这种情况下,不会调用 success 函数。

如果您打印错误回调的消息,您可以验证这一点。 使用以下代码:

url = url + "?" + $(".ajaxForm").serialize();
$(".ajaxForm").ajax({url: url, dataType: "json", type : "post",
                    success: function(response) {},
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(textStatus);
                        console.log(errorThrown);
                    }});

打印输出之一应该类似于“解析器错误”。

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:

url = url + "?" + $(".ajaxForm").serialize();
$(".ajaxForm").ajax({url: url, dataType: "json", type : "post",
                    success: function(response) {},
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(textStatus);
                        console.log(errorThrown);
                    }});

One of the print-outs should be something like "parser error".

黑寡妇 2024-07-28 10:55:59

我认为您需要表单的 url 和 post 类型才能将数据发送到某处?

这就是他们在 jquery.com 上的设置方式:

$("#myform").ajaxForm({
   url: "mypage.php",
   type: "POST"
 });

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:

$("#myform").ajaxForm({
   url: "mypage.php",
   type: "POST"
 });
遗心遗梦遗幸福 2024-07-28 10:55:59

ajaxErrorHandler 是否在其他地方定义?
我尝试了你的代码,它运行得很好。 您使用什么版本的 jQuery 和 jQuery 表单?

这是我尝试过的代码。 与此测试位于同一目录中的包含“{test:'hello world'}”的名为“test.json”的文件:

<script type="text/javascript" src="http://malsup.com/jquery/jquery-1.2.6.js"></script>
<script type="text/javascript" src="http://malsup.com/jquery/form/jquery.form.js?2.28"></script>

<form class="ajax_form" action="test.json" method="post"><input type="text" name="q" /><input type="submit" /></form>
<br/>
<form class="ajax_form" action="test.json" method="post"><input type="text" name="q" /><input type="submit" /></form>

<script>
$('.ajax_form').ajaxForm({
  dataType: 'json',
  error: function() {alert("error");},
  success: function(response) {alert(response.test);}
});
</script>

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:

<script type="text/javascript" src="http://malsup.com/jquery/jquery-1.2.6.js"></script>
<script type="text/javascript" src="http://malsup.com/jquery/form/jquery.form.js?2.28"></script>

<form class="ajax_form" action="test.json" method="post"><input type="text" name="q" /><input type="submit" /></form>
<br/>
<form class="ajax_form" action="test.json" method="post"><input type="text" name="q" /><input type="submit" /></form>

<script>
$('.ajax_form').ajaxForm({
  dataType: 'json',
  error: function() {alert("error");},
  success: function(response) {alert(response.test);}
});
</script>
︶ ̄淡然 2024-07-28 10:55:59

好吧,我检查了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文