Return jQuery AJAX output as return if a function
I called a web serivce via jQuery AJAX. web service returned 'True' as output.
I want to return as return of a function. but it returns undefined. Where is the source of problem ? Function IsAuthenticated returns undefined however response.d contains 'True' value (its string). function IsAuthenticated() {
//Todo Check If user logined or not from server
return CallWebService("Server/WebServices/Server.asmx/IsUserLogined", '{}');
}
function CallWebService(WebServicePath, parameters) {
$.ajax({
type: "POST",
url: WebServicePath,
data: parameter开发者_如何学Gos,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
if (response.d == 'True') {
return true;
}
else
{ return false; }
},
failure: function (msg) {
alert("error");
}
});
I see that you are using async:false option to make the ajax calls synchronous.
However the success function is still a callback and returning true from the success handler will not return the value as a result of the ajax function call.
You need to capture the output into a local variable and return it.
Try this:
function CallWebService(WebServicePath, parameters) {
var retValue = null;
$.ajax({
type: "POST",
url: WebServicePath,
data: parameters,
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
if (response.d == 'True') {
retValue = true;
}
else
{ retValue = false; }
},
failure: function (msg) {
alert("error");
}
});
return retValue;
}
精彩评论