[英]jQuery $.ajax done callback not firing
I have the following code :我有以下代码:
$("#loginSubmitButton").on("click",function(){
var loginUserDetails = {
email: $("#email").val(),
password: $("#password").val()
};
//Send the AJAX request to authenticate the user
$.ajax({
type: "POST",
url: "/somewebservice/v1/users/authenticate",
data: JSON.stringify(loginUserDetails),
contentType: "application/json;charset=UTF-8",
dataType: "json",
}).done(function() {
$("#loginResult").text("Login successful");
})
.fail(function() {
$("#loginResult").text("Login failed");
});
});
As per the jquery documentation (unless I am understanding something incorrectly) I expect the done to be fired if I receive a 200 OK from my web server.根据 jquery 文档(除非我理解错误),如果我从我的 Web 服务器收到 200 OK,我希望 done 被触发。 However, in chrome console I can see a 200 OK response but jquery seems to fire the fail handler.
但是,在 chrome 控制台中,我可以看到 200 OK 响应,但 jquery 似乎触发了失败处理程序。
Does anyone have any idea what I might be doing wrong here?有谁知道我在这里可能做错了什么?
您需要删除:
dataType: "json"
There are lots of suggestions to remove有很多建议可以删除
dataType: "json"
While I grant that this works it's probably ignoring the underlying issue.虽然我承认这是有效的,但它可能忽略了潜在的问题。 It's most likely caused by a parser error (the browser parsing the json response).
这很可能是由解析器错误(浏览器解析 json 响应)引起的。 Firstly examine the XHR parameter in either .always() or .fail().
首先检查 .always() 或 .fail() 中的 XHR 参数。
Assuming it is a parser fail then why?假设它是一个解析器失败,那么为什么? Perhaps the return string isn't JSON.
也许返回字符串不是 JSON。 Or it could be errant whitespace at the start of the response.
或者它可能是响应开始时的错误空格。 Consider having a look at it in fiddler.
考虑在 fiddler 中查看它。 Mine looked like this:
我的看起来像这样:
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
{"type":"scan","data":{"image":".\/output\/ou...
In my case this was a problem with PHP spewing out unwanted characters (in this case UTF file BOMs).就我而言,这是 PHP 喷出不需要的字符(在这种情况下为 UTF 文件 BOM)的问题。 Once I removed these it fixed the problem while also keeping
一旦我删除了这些,它就解决了问题,同时还保持
dataType: json
If your server returns empty string for a json response (even if with a 200 OK), jQuery treats it as failed.如果您的服务器为 json 响应返回空字符串(即使是 200 OK),jQuery 会将其视为失败。 Since v1.9, an empty string is considered invalid json.
从 v1.9 开始,空字符串被认为是无效的 json。
Whatever is the cause, a good place to look at is the 'data' parameter passed to the callbacks:无论是什么原因,一个值得关注的地方是传递给回调的“数据”参数:
$.ajax( .. ).always(function(data) {
console.log(JSON.stringify(data));
});
Its contents will give you an understanding of what's wrong.它的内容将使您了解问题所在。
需要删除 , from dataType: "json",
dataType: "json"
The ajax URL must be the same domain. ajax URL 必须是同一个域。 You can't use AJAX to access cross-domain scripts.
您不能使用 AJAX 访问跨域脚本。 This is because of the Same Origin Policy.
这是因为同源策略。
add "dataType:JSONP" to achieve cross domain communication添加“dataType:JSONP”实现跨域通信
use below code使用下面的代码
$.ajax({
URL: cross domain
dataType: 'jsonp'
// must use dataType:JSONP to achieve cross domain communication, otherwise done function would not called.
// jquery ajax will return "statustext error" at }).always(function(data){}
}).always(function(data){
alert(JSON.stringify(data));
}
A few things that should clear up your issue and a couple hints in general.一些应该可以解决您的问题的事情以及一些提示。
Don't listen for a click on a submit button.不要听点击提交按钮。 You should wait for the submit event on the form.
您应该等待表单上的提交事件。
The data
option for $.ajax
isn't expecting a JSON string. $.ajax
的data
选项不需要 JSON 字符串。 It wants a serialized string or an array with name and value objects.它需要一个序列化的字符串或一个带有名称和值对象的数组。 You can create either of those easily with
.serialize()
or .serializeArray()
.您可以使用
.serialize()
或.serializeArray()
轻松创建其中任何一个。
Here is what I was thinking for your script.这是我对你的脚本的想法。
$('#form-with-loginSubmitButton').on('submit', function(e){
e.preventDefault():
var $form = $(this),
data = $form.serializeArray();
$.ajax({
type: "POST",
url: "/somewebservice/v1/users/authenticate",
data: data
}).done(function(result){
console.log(result);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.