作者:xiaobei爱吃肉 | 来源:互联网 | 2023-05-18 03:38
I have a form which submit a form via AJAX with :remote => true. Looking at the server log and FireBug, I get the response 200 OK and it returns JSON in the form of:
我有一个表单,通过AJAX提交表单:remote => true。查看服务器日志和FireBug,我得到200 OK的响应,它以下列形式返回JSON:
{ "email": "test@test.com"}
then I have these two handlers:
然后我有这两个处理程序:
$('#new_invitation').bind("ajax:success", function(event, data, status, xhr) {
alert('test');
});
$('#new_invitation').bind("ajax:error", function() {
alert('error');
});
and even if I get back a 200OK, it is the error handler that fires. The only time I managed to make the success handler work was when I send a empty response with 200 in the header.
即使我回到200OK,它也会触发错误处理程序。我设法让成功处理程序工作的唯一一次是我在标题中发送一个200的空响应。
I can't figure out why this isnt working :-S
我无法弄清楚为什么这不起作用:-S
EDIT 1------------ After doing these changes:
编辑1 ------------完成这些更改后:
$('#new_invitation').bind("ajaxSuccess", function(event, data, status, xhr) {
alert('test');
});
$('#new_invitation').bind("ajaxError", function(jqXHR, textStatus, errorThrown) {
alert('error');
console.log(jqXHR.responseText);
console.log(textStatus.responseText);
console.log(errorThrown.responseText);
});
I am still getting the same error. The log stuff gives me:
我仍然得到同样的错误。日志的东西给了我:
undefined
my_email@test.com
undefined
Here is the code for the form (standard Rails stuff):
这是表单的代码(标准Rails的东西):
<%= form_for @shoot.invitations.new, :url=>shoot_invitations_path(@shoot), :remote => true, :html => {:class => 'form-inline'} do |f| %>
<%= f.text_field :email, :'placeholder' => 'ex: test@test.com' %>
<%= f.text_field :role, :'placeholder' => 'ex: Photographer' %>
<%= f.submit "Invite", :class => 'btn btn-success' %>
<% end %>
EDIT 2 ---------
编辑2 ---------
I did a few changes and now it seems my error is a parse error. I dont understand because this is the JSON I am getting back from the server (data.responseText), which seems all good:
我做了一些更改,现在看来我的错误是一个解析错误。我不明白,因为这是我从服务器(data.responseText)回来的JSON,这似乎都很好:
{"email":"zxxczxc@test.com"}
ANSWER --------- I managed to have everything work when I put :'data-type' => :json in the form options. I tried this before and it did not work because I put it in the form_tag options and not the html options...
答案---------当我把'data-type'=>:json放在表单选项中时,我设法让一切正常。我之前尝试过这个并没有用,因为我把它放在form_tag选项而不是html选项中......
4 个解决方案