错误运行即使响应是OK

错误运行即使响应是OK

本文介绍了jQuery的阿贾克斯:错误运行即使响应是OK 200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过AJAX与提交表单形式:远程=>真。纵观服务器日志和萤火虫,我得到的回应200 OK并返回JSON形式为:

  {电子邮件:[email protected]}
 

然后我有这两个处理程序:

  $('#new_invitation')绑定(AJAX:成功,功能(事件,数据,身份,XHR){
    警报(测试);
});

$('#new_invitation')绑定(AJAX:错误,函数(){
    警报('错误');
});
 

即使我得到一个200 OK,这是错误处理程序火灾。唯一一次我设法让成功的处理工作是当我发送200在头一个空的响应。

我想不通为什么这个心不是工作:-S

编辑1 ------------在做了这些改变后:

  $('#new_invitation)。绑定(的ajaxSuccess功能(事件,数据,身份,XHR){
    警报(测试);
});

$('#new_invitation)。绑定(ajaxError功能(jqXHR,textStatus,errorThrown){
警报('错误');
    执行console.log(jqXHR.responseText);
    执行console.log(textStatus.responseText);
    执行console.log(errorThrown.responseText);
});
 

我仍然得到同样的错误。日志的东西给我:

 未定义
[email protected]
未定义
 

下面是code的形式(标准Rails的东西):

 <%=的form_for @ shoot.invitations.new,:URL => shoot_invitations_path(@shoot):远程=>如此,:HTML => {:类=> 表格内联'}办| F | %>
    <%= f.text_field:电子邮件:'占位'=> 恩:[email protected]%>
    <%= f.text_field:角色:'占位'=> 恩:摄影师%>
    <%= f.submit邀请,:类=> BTN BTN-成功%>
<%结束%GT;
 

编辑2 ---------

我做了一些变化,现在看来我的错误是一个语法错误。我不明白,因为这是JSON我从服务器获取(data.responseText),这似乎都好回:

  {电子邮件:[email protected]}
 

答---------我设法把一切工作的时候,我把:数据类型=>:JSON形式的选项。之前,我尝试这样做,也没有工作,因为我把它放在的form_tag选项,而不是HTML选项...

解决方案
  1. 检查$阿贾克斯的数据类型设置为JSONP

  2. 试图返回{EMAIL:[email protected]}

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:

{ "email": "[email protected]"}

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.

I can't figure out why this isnt working :-S

EDIT 1------------After doing these changes:

$('#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
[email protected]
undefined

Here is the code for the form (standard Rails stuff):

<%= form_for @shoot.invitations.new, :url=>shoot_invitations_path(@shoot), :remote => true, :html => {:class => 'form-inline'} do |f| %>
    <%= f.text_field :email, :'placeholder' => 'ex: [email protected]' %>
    <%= f.text_field :role, :'placeholder' => 'ex: Photographer' %>
    <%= f.submit "Invite", :class => 'btn btn-success' %>
<% end %>

EDIT 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:

{"email":"[email protected]"}

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...

解决方案
  1. Check that $.ajax's datatype is set to jsonp

  2. Try to return {email:"[email protected]"}

这篇关于jQuery的阿贾克斯:错误运行即使响应是OK 200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:07