I am creating an ajax call like so:
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,
});
However when I repeat the request with identical parameters, I sometimes get the success callback and sometimes get the error callback. When the error callback is called, the jqXHR.status is always 0 on a failure, but there is nothing descriptive in statusText, responseText, textStatus or errorThrown.
I did a tcpdump on the HTTP requests and the request looks like:
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
The response looks like:
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}
I am at a loss to why it is sometimes succeeding and sometimes failing.
The problem was I was using ajax to submit a form. When I clicked the submit button it would work fine, but when I hit enter in a text input field it would encounter the errors. Apparently hitting enter in a input field was causing the form to submit twice. The fix was to set onsubmit="return false;" for the form to prevent double submission.
精彩评论