Ajax调用显示错误无法调试

时间:2016-05-09 17:30:54

标签: javascript ajax asp.net-mvc

这就是javascript的样子

 <script type="text/javascript">
  $(document).ready(function () {
    $('#loginButton').click(function () {
        //this.disabled = true;
        debugger;
        var data = {
            "userid": $("#username").val(),
            "password": $("#password").val()
        };
        $.ajax({
            url: "/Account/LoginPost",
            type: "POST",
            data: JSON.stringify(data),
            dataType: "json",
            contentType: "application/json",
            success: function (response) {
                if (response.Success) {
                    $.get("@Url.Action("Search", "Home")", function (data) {
                        $('.container').html(data);
                    });
                }
                else
                    window.location.href = "@Url.Action("Index", "Home")";
            },
            error: function () {
                 alert('Login Fail!!!');
            }
        });
    });
 });

我收到警报('登录失败'),调试器也没有被点击。 我正在使用jquery 1.9.1并且包含了不引人注目的

我的控制器是这样的,因为你可以传递字符串值而不是对象值 到控制器所以stringify在这里是合理的

[HttpPost]
public JsonResult LoginPost(string userid, string password)
{
    using (someentities wk = new someentities())
    {
        var LoginUser = wk.tblUsers.Where(a => a.Username.Equals(userid)&&a.Password.Equals(password)).FirstOrDefault();
        if (LoginUser != null)
        {
            FormsAuthentication.SetAuthCookie(userid,false);
            Session["Username"] = LoginUser.Username;
            Session["Password"] = LoginUser.Password;
            Session["Name"] = LoginUser.Name;
            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }
        else
        {
            TempData["Login"] = "Please Enter Correct Login Details";
            return Json(new { Success = false }, JsonRequestBehavior.AllowGet);
        }
    }
    // If we got this far, something failed, redisplay form
}

页面加载时会显示这些错误

  

$(..)live不是有效的函数   (匿名函数)@jquery.unobtrusive-ajax.js:115   (匿名函数)@jquery.unobtrusive-ajax.js:163

2 个答案:

答案 0 :(得分:0)

看一下成功函数

           success: function (response) {
                if (response.Success) {
                    $.get("@Url.Action("Search", "Home")", function (data) {
                        $('.container').html(data);
                    });
                }
                else
                    window.location.href = "@Url.Action("Index", "Home")";
            }

您正在使用多个,将其与单个'结合使用,这是一个语法错误,请尝试检查编辑器(如Atom)上的代码,避免这种错误

答案 1 :(得分:0)

Stringifyobject转换为string。您是否尝试过data object而不是string?尝试将JSON.stringify(data),替换为data