gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:32:22 25 4
gpt4 key购买 nike

我有一个 MVC 4 应用程序登录页面,在其中我尝试对登录 Controller 使用 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;
});

我的身份验证操作的索引 Controller :

// 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 请求时,错误位都会运行。这是来自 firebug 的响应 header :

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"}]

当我在 Controller 上放置断点并进行调试时,它永远不会触发,而是收到 json.parse 语法错误。我似乎不明白为什么。我已经在这个网站和其他网站上尝试了很多其他解决方案,但它们似乎对我不起作用。我肯定错过了什么。

最佳答案

您应该更改您的操作方法:

    [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);
}

关于javascript - MVC 4 Razor Jquery Ajax 返回 JSON.parse() 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com