Ответ 1
измените свой код на
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;//doesnt goes here
},
error: function (textStatus, errorThrown) {
Success = false;//doesnt goes here
}
});
//done after here
return Success;
}
Вы можете вернуть значения только из функции synchronous
. В противном случае вам нужно будет сделать callback
.
Итак, я просто добавил async:false,
к вашему вызову ajax
Update:
jquery ajax-вызовы по умолчанию асинхронны. Таким образом, функции успеха и ошибки будут вызваны при завершении загрузки ajax. Но ваш оператор return будет выполнен сразу после запуска вызова ajax.
Лучшее aproach будет
// 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);
}
и вызовите ajax как
// Callback is the callback-function that needs to be called when asynchronous call is complete
ChangePurpose(Vid, PurId, Callback);