jquery -ajax 不检查来自控制器的布尔值
jquery -ajax not check the boolean value from controller
我有一个控制器操作,它是 return 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 代码调用此控制器
$.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 结果为真然后要工作 css。但这不起作用,请帮助我。
result
是一个变量名,只存在于那个action方法中。它不会包含在 JSON.
中
我很确定您的布尔值将存储在 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);
我有一个控制器操作,它是 return 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 代码调用此控制器
$.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 结果为真然后要工作 css。但这不起作用,请帮助我。
result
是一个变量名,只存在于那个action方法中。它不会包含在 JSON.
我很确定您的布尔值将存储在 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);