我想从此方法读取返回值:
public ActionResult ClientIsBlocked(int? clientId)
{
if (!clientId.HasValue)
return Json(null);
bool isBlocked = false;
try
{
isBlocked = this.clientsProvider.GetClientById(clientId.Value).IsBlocked;
}
catch
{
// logg
}
return Json(isBlocked);
}
在我的视图中的java脚本中。它应该是async / ajax。怎么做? 这是我的js代码。
function isBlocked(id) {
$.ajax({
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "GET",
url: '@Url.Action("ClientIsBlocked", "CustomerManagement", new { Area = "CustomerManagement" })',
data: JSON.stringify({ 'clientId': id }),
success: function(data) {
if(!data.success) {
}
}
})
答案 0 :(得分:1)
在您的行动变更中:
return Json(isBlocked);
为:
return Json(isBlocked,JsonRequestBehavior.AllowGet);
否则你的ajax可能会失败抛出异常:
' /'中的服务器错误应用强>
此请求已被阻止,因为在GET请求中使用此信息时,可能会向第三方网站披露敏感信息。要允许GET请求,请将JsonRequestBehavior设置为AllowGet。
并在成功回调的js中:
success: function(data) {
alert(data); // data is the bool that is returned by action
}