提问者:小点点

在ASP.NET MVC中未调用jQuery Ajax成功函数


我有一个Bootstrap模态表单来检查IMO的存在,并且我正在使用jQuery Ajax Post来提交它。

null

$('#submitCheck').click(function() {
   $.ajax({
     type: "POST",
     url: "/Entry/Index",
     dataType: 'json',
     success: function(data) {
       alert("Check");
       if (data == 'true') {
         alert("RETURN TRUE");
         window.location.href = "/Entry/Create";
       } else {
         alert("RETURN FALSE");
         $('#divStat').html("IMO does not exist");
       }

     },
     error: function() {
       alert("Something went wrong");
     }
   });
 })

null

这里是控制器:

null

[HttpPost]
public ActionResult Index(string IMO) {
  var data = db.Ships.Where(d => d.IMO.Equals(IMO)).FirstOrDefault();
  if (data != null)
    return Json(true);
  return Json(false);
}

null

当我提交表单时,IMO被选中,并且我看到结果在视图中为“true”(或“false”),但是在jQuery代码中,没有调用success函数,所以我看不到“alert”或其他任何东西。我做错了什么?


共1个答案

匿名用户

您试图检查字符串“true”还是布尔值true?如果删除引号,则该值应为true。
此外,您可能希望执行以下操作:
返回Json(new{success=true},JSONRequestBehavior.AllowGet);以便执行以下操作:

success: function (data) {
            //or if(data.success) which evaluates to true.
            if (data.success == true) {
                alert("RETURN TRUE");
            }