我正在尝试使用ajax进行一些验证。它从我的MVC3控制器调用一个方法,但无论控制器返回true还是false,它都返回true。始终显示警报。
ClientChoices / HasJobInProgress
public Boolean HasJobInProgress(int clientId)
{
return false;
//return _proxy.GetJobInProgress(clientId);
}
Jquery.Ajax
$("#saveButton").click(function() {
var hasCurrentJob =
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: @Model.ClientId },
success: function(data){
return data;
}
});
if (hasCurrentJob) {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
}
});
的解 的
$("#saveButton").click(function() {
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: '@Model.ClientId' },
success: function(data){
showMsg(data);
},
cache: false
});
});
function showMsg(hasCurrentJob) {
if (hasCurrentJob.toString()=="True") {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
}
}
答案 0 :(得分:3)
AJAX调用是异步的,您的返回数据不会设置hasCurrentJob
的值,而是返回成功回调。请参阅下文,了解如何根据data
显示提醒。
$("#saveButton").click(function() {
//var hasCurrentJob
$.ajax({
url: '@Url.Action("HasJobInProgress", "ClientChoices")/',
data: { id: @Model.ClientId },
success: function(data){
showMsg(data);
}
});
});
function showMsg(hasCurrentJob) {
if (hasCurrentJob === 'true') {
alert("The current clients has a job in progress. No changes can be saved until current job completes");
}
}