Ala*_*ain 10 javascript ajax jquery ruby-on-rails-3
我有一个表单,通过AJAX提交表单:remote => true.查看服务器日志和FireBug,我得到200 OK的响应,它以下列形式返回JSON:
{ "email": "test@test.com"}
Run Code Online (Sandbox Code Playgroud)
然后我有这两个处理程序:
$('#new_invitation').bind("ajax:success", function(event, data, status, xhr) {
alert('test');
});
$('#new_invitation').bind("ajax:error", function() {
alert('error');
});
Run Code Online (Sandbox Code Playgroud)
即使我回到200OK,它也会触发错误处理程序.我设法让成功处理程序工作的唯一一次是我在标题中发送一个200的空响应.
我无法弄清楚为什么这不起作用:-S
编辑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);
});
Run Code Online (Sandbox Code Playgroud)
我仍然得到同样的错误.日志的东西给了我:
undefined
my_email@test.com
undefined
Run Code Online (Sandbox Code Playgroud)
这是表单的代码(标准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 %>
Run Code Online (Sandbox Code Playgroud)
编辑2 ---------
我做了一些更改,现在看来我的错误是一个解析错误.我不明白,因为这是我从服务器(data.responseText)回来的JSON,这似乎都很好:
{"email":"zxxczxc@test.com"}
Run Code Online (Sandbox Code Playgroud)
答案---------当我放置时,我设法让一切正常:在表单选项中输入'data-type'=>:json.我之前尝试过这个并没有用,因为我把它放在form_tag选项而不是html选项中......
Dri*_*Dan 18
如果服务器返回一些无效的JSON,例如单个空格,jQuery将生成一个解析错误,并将其视为失败的请求,即使状态代码为200.
从jQuery 1.9开始,当类型设置为JSON时,完全空的响应被视为失败的请求,因为空字符串是无效的JSON.请参阅http://jquery.com/upgrade-guide/1.9/#jquery-ajax-returning-a-json-result-of-an-empty-string.
归档时间: |
|
查看次数: |
9403 次 |
最近记录: |