我有一个 Controller 操作,它将 bool 结果返回给 jquery。
[HttpGet]
public ActionResult IsVoucherValid(string voucherCode)
{
bool result = false;
var voucher = new VoucherCode(voucherCode);
if(voucher.Status==0)
{
result = true;
}
return Json(result);
}
并使用ajax代码调用此 Controller
$.ajax({
url: '/Account/IsVoucherValid?voucherCode=' + code,
type: 'Get',
contentType: 'application/json;',
success: function (data) {
alert("success");
if (data) {
//if result=true, want to work this
$("#person-data").css({ "display": "block" });
}
},
error:alert("error")
});
在ajax成功后,json结果为true,然后想要使用css。但这不起作用,请帮助我。
最佳答案
result
是仅存在于该操作方法中的变量名称。它不会包含在 JSON 中。
我非常确定您的 bool 值将存储在 data
中,因为您只发回一个值:
$.ajax({
url: '/Account/IsVoucherValid?voucherCode=' + code,
type: 'Get',
contentType: 'application/json;',
success: function (data) {
if (data) { //if result=true, want to work this
$("#person-data").css({ "display": "block" });
}
}
});
如果有疑问,请执行 console.log(data)
查看其中包含的内容。在向我们提出问题之前,您至少应该进行最少的调试。
此外,正如@Stephen Muecke 在下面指出的那样,如果您使用 GET 检索此数据,则需要使用:
return Json(result, JsonRequestBehavior.AllowGet);
关于javascript - jquery -ajax 不检查 Controller 中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28892911/