问题描述
我使用以下ajax函数调用Web服务,如果出现错误,我希望它触发JavaScript函数userCreate()
I use the following ajax function to call a webservice, if I get an error I would like it to trigger a javascript function userCreate()
$.ajax({
type:"POST",
beforeSend: function (request) {
request.setRequestHeader("X-DreamFactory-Application-Name", "snapcentral");
request.setRequestHeader("X-DreamFactory-Session-Token", sessID.dream);
},
url: url,
data: '{"username":"' + userStats.loginID + '","lithid":"' + userStats.userID + '"}',
success: function(data) {
dbData = data;
console.log('createUsers Executed sucessfully');
},
error: function (request, status, error) {
console.log("createUser: " + request.responseText);
createUser();
}
});
除错误"部分未触发createUser()函数外,所有其他操作均按预期进行.我从根本上做错了什么,如何获得查询以在错误时执行功能.
Everything works as expected apart from the "error" section not triggering the createUser() function. What am I doing fundamentally wrong and how do I get the query to execute a function on error.
如果我独立调用,我为createUser编写的函数将起作用.
The function I have written for createUser works if I call standalone..
最欢迎您提供建议和/或指导-谢谢.
Advise and/or direction would be most welcome - thanks.
推荐答案
仅当服务器以错误代码响应或完全不响应时,才调用jQuery的error
回调.如果您以200
响应并在响应中出现错误消息,则将调用success
回调.可悲的是,jQuery并不那么聪明.
jQuery's error
callback is only called if the server responds with an error code, or doesn't respond at all. If you respond with 200
and an error message in your response, the success
callback will be called. Sadly, jQuery isn't that smart.
相反,您可以返回错误代码,也可以在success
回调中检查是否有错误消息.
Instead, you can either return an error code or check in your success
callback if there's a error message.
这篇关于在Ajax错误时执行javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!