2011-08-31 47 views
0

间歇性故障我创建Ajax调用就像这样:jQuery的AJAX调用挂架上相同的请求

var form = $("#form"); 
    $.ajax({url: "/url/create_web_registration?invite_token=abc", 
      data: form.serialize(), 
      type: "POST", 
      dataType: "json", 
      success: $.web_registration.index.registerSubmitSuccess, 
      error: $.web_registration.index.registerSubmitError, 
    }); 

然而,当我重复使用相同的参数要求,我有时会得到成功的回调,有时得到的错误回电话。当调用错误回调时,jqXHR.status在失败时始终为0,但在statusText,responseText,textStatus或errorThrown中没有任何描述。

我做的HTTP请求tcpdump和要求的样子:

POST /shopkick/v1/user/create_web_registration?invite_token=abc HTTP/1.1 
Host: [redacted]:5000 
Connection: keep-alive 
Referer: http://[redacted]:5000/wr2/abc?invite_token=&zip_code=94123&phone_number=%2B16785556982&facebook-form= 
Content-Length: 169 
Origin: http://[redacted]:5000 
X-Requested-With: XMLHttpRequest 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.215 Safari/535.1 
Content-Type: application/x-www-form-urlencoded 
Accept: application/json, text/javascript, */*; q=0.01 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 

invite_token=&zip_code=94123&phone_number=%2B16785552982 

的回应是这样的:

HTTP/1.0 200 OK 
Server: PasteWSGIServer/0.5 Python/2.6.1 
Date: Wed, 31 Aug 2011 05:34:10 GMT 
Pragma: no-cache 
Cache-Control: no-cache 
Content-type: text/plain 
Content-Length: 74 

{"error_message": "Facebook account already registered", "success": false} 

我很茫然,为什么它有时成功,有时失败。

+0

不* Facebook账号已经注册* *给你一些提示? –

+0

“Facebook帐户已注册”是我的服务器代码的回复。 JQuery不理解我从服务器传回来的JSON对象。只要它是HTTP 200,它应该调用传递JSON对象的成功处理程序作为数据参数。事实上,它有时会用完全相同的HTTP响应调用成功处理程序。 – jwegan

+0

为什么在响应中你设置了'Content-type:text/plain'并且仍然在响应体中发送一个JSON?这看起来不正确。如果您打算发送JSON响应,请将正确的“Content-Type”标头设置为“application/json”。 –

回答

0

问题是我使用ajax提交表单。当我点击提交按钮时,它会正常工作,但是当我在文本输入字段中输入时,它会遇到错误。显然在输入字段中输入导致表单提交两次。解决的办法是设置onsubmit =“return false;”为防止双重提交的表单。