繁体   English   中英

jQuery -ajax不检查控制器的布尔值

[英]jquery -ajax not check the boolean value from controller

我有一个控制器动作,它将布尔结果返回到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结果为true,则需要使用CSS。 但这不起作用,请帮助我。

result是仅在该操作方法中存在的变量名称。 它不会包含在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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM