MVC 4 Razor Jquery Ajax返回JSON.parse()语法错误

时间:2014-02-11 02:03:37

标签: javascript jquery ajax asp.net-mvc json

我有一个MVC 4应用程序登录页面,其中我尝试使用jquery ajax请求到我的登录控制器以查看用户是否存在。我的jquery:

$(document).ready(function () {
        $("#btnSubmit").click(function () {

            var actionUrl = '@Url.Action("Index", "Auth")';
            var username = $("#UserName").val();
            var password = $("#Password").val();
            var jsonString = '[{"username": "' + username + '", "password": "' + password + '"}]';

            $.ajax({
                type: 'POST',
                contentType: 'application/json; charset=utf-8;',
                dataType: 'json',
                url: actionUrl,
                data: jsonString,
                success: function (data) {                        
                    alert(data);
                },
                error: function (xhr, ajaxOptions, thrownError) {                    
                    alert(xhr.status + " - " + thrownError);
                    alert(ajaxOptions);
                }
            });

            return false;
        }); 

我的身份验证操作上的索引控制器:

// POST: /login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public JsonResult Index(LoginModel model, string returnUrl)
        {                
            return this.Json(String.Empty);                          
        }

我的路线配置:

routes.MapRoute(
                name: "AuthRoot",
                url: "login",
                defaults: new { controller = "Auth", action = "Index", returnUrl = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "AuthRoot2",
                url: "login/{username}/{password}",
                defaults: new { controller = "Auth", action = "Index", returnUrl = UrlParameter.Optional, username = UrlParameter.Optional, password = UrlParameter.Optional }
            );

我不确定这里发生了什么,因为每次生成ajax请求时,on错误位都会运行。这是来自firebug的响应头:

Response Headers
Cache-Control   private
Connection  Close
Content-Length  3835
Content-Type    text/html; charset=utf-8
Date    Tue, 11 Feb 2014 01:51:34 GMT
Server  ASP.NET Development Server/11.0.0.0
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 4.0

Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Type    application/json; charset=utf-8;
Cookie  __RequestVerificationToken=GqfLsAL26Vo4UM6PiXwON-zGO8M4Y9v1WBA4XQ8tuxDtvPiW7aQy5JKIP85ULQawEoPeB2F0XDSsxd0jsl8braOxMEAkXR1V1LAgdmxBe5iQDy_1qOfX-fjPpebgHgM2Qyq8kv1pa-y4cjfmYhSthw2
Host    localhost:50925
Referer http://localhost:50925/login
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:27.0) Gecko/20100101 Firefox/27.0
X-Requested-With    XMLHttpRequest

这就是我发布的内容:

JSON            

0
    Object { username="manager", password="P@ssw0rd"}

password
    "P@ssw0rd"

username
    "manager"
Source
[{"username": "manager", "password": "P@ssw0rd"}]

当我在控制器和调试上放置一个断点时,它永远不会触发,而是我收到一个json.parse语法错误。我似乎无法弄清楚为什么。我在这个网站和其他网站上尝试了很多其他解决方案,但它们似乎并不适合我。我肯定错过了什么。

1 个答案:

答案 0 :(得分:0)

你应该改变你的Action方法:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public JsonResult Index(LoginModel model)
    {                
        return this.Json(String.Empty);                          
    }

   or if above not work then use following:
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public JsonResult Index(string username,string password)
    {                
        return this.Json(String.Empty);                          
    }