Javascript Jquery Ajax 调用,不调用成功或错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14475853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Ajax Call, doesn't call Success or Error
提问by Ruchan
Possible Duplicate:
How do I return the response from an asynchronous call?
可能的重复:
如何从异步调用返回响应?
I am using Jquery Ajax to call a service to update a value.
我正在使用 Jquery Ajax 调用服务来更新值。
function ChangePurpose(Vid, PurId) {
var Success = false;
$.ajax({
type: "POST",
url: "CHService.asmx/SavePurpose",
dataType: "text",
data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
contentType: "application/json; charset=utf-8",
success: function (data) {
Success = true;//doesn't go here
},
error: function (textStatus, errorThrown) {
Success = false;//doesn't go here
}
});
//done after here
return Success;
}
and Service:
和服务:
[WebMethod]
public string SavePurpose(int Vid, int PurpId)
{
try
{
CHData.UpdatePurpose(Vid, PurpId);
//List<IDName> abc = new List<IDName>();
//abc.Add(new IDName { Name=1, value="Success" });
return "Success";
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
the service is being called Successfully from the AJAX. Value is also being Changed.But after the Service, success:or error:functions are not being called, in this case success should have been called but it is not working.
该服务正在从 AJAX 成功调用。价值也在改变。但是在 Service 之后,success:或error:functions are not being called,在这种情况下应该调用成功但它不起作用。
I used firebug and found that, the success or error functions are being skipped and goes directly to return Success;
我用过firebug发现,成功或错误功能被跳过并直接进入 return Success;
Can't seem to find what's the problem with the code.
似乎无法找到代码有什么问题。
Update:adding async: falsefixed the problem
更新:添加async: false修复了问题
回答by Wolf
change your code to:
将您的代码更改为:
function ChangePurpose(Vid, PurId) {
var Success = false;
$.ajax({
type: "POST",
url: "CHService.asmx/SavePurpose",
dataType: "text",
async: false,
data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
contentType: "application/json; charset=utf-8",
success: function (data) {
Success = true;
},
error: function (textStatus, errorThrown) {
Success = false;
}
});
//done after here
return Success;
}
You can only return the values from a synchronousfunction. Otherwise you will have to make a callback.
您只能从synchronous函数返回值。否则你将不得不制作一个callback.
So I just added async:false,to your ajax call
所以我刚刚添加async:false,到你的 ajax 调用中
Update:
更新:
jquery ajax calls are asynchronous by default. So success & error functions will be called when the ajax load is complete. But your return statement will be executed just after the ajax call is started.
jquery ajax 调用默认是异步的。所以在ajax加载完成后会调用success&error函数。但是您的 return 语句将在 ajax 调用开始后立即执行。
A better approach will be:
更好的方法是:
// callbackfn is the pointer to any function that needs to be called
function ChangePurpose(Vid, PurId, callbackfn) {
var Success = false;
$.ajax({
type: "POST",
url: "CHService.asmx/SavePurpose",
dataType: "text",
data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
contentType: "application/json; charset=utf-8",
success: function (data) {
callbackfn(data)
},
error: function (textStatus, errorThrown) {
callbackfn("Error getting the data")
}
});
}
function Callback(data)
{
alert(data);
}
and call the ajax as:
并将 ajax 称为:
// Callback is the callback-function that needs to be called when asynchronous call is complete
ChangePurpose(Vid, PurId, Callback);
回答by Tom Sarduy
Try to encapsulate the ajax call into a function and set the async option to false. Note that this option is deprecated since jQuery 1.8.
尝试将ajax调用封装成一个函数,并将async选项设置为false。请注意,此选项自 jQuery 1.8 起已弃用。
function foo() {
var myajax = $.ajax({
type: "POST",
url: "CHService.asmx/SavePurpose",
dataType: "text",
data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
contentType: "application/json; charset=utf-8",
async: false, //add this
});
return myajax.responseText;
}
You can do this also:
你也可以这样做:
$.ajax({
type: "POST",
url: "CHService.asmx/SavePurpose",
dataType: "text",
data: JSON.stringify({ Vid: Vid, PurpId: PurId }),
contentType: "application/json; charset=utf-8",
async: false, //add this
}).done(function ( data ) {
Success = true;
}).fail(function ( data ) {
Success = false;
});
You can read more about the jqXHR jQuery Object
您可以阅读有关jqXHR jQuery 对象的更多信息

