使用jQuery后张贴到Asp.net 4 MVC 2的空数据 [英] data posted with jquery post to Asp.net 4 Mvc 2 as null
问题描述
我使用jQuery 1.8将数据发布到ASP.Net MVC 4 2这样
I am using jquery 1.8 to post data to ASP.Net 4 MVC 2 like this
PostData('Home/Login',{ "username": $("#username").val(),"password": $("#password").val()})
function PostData(url, data) {
$.ajax({
url: url,
data:data,
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
Success:successFunction, Error: ErrorFunction
});
};
我的模型
namespace FmsMvc.Models
{
public class UsersModel
{
public int UserID { get; set; }
public string Role { get; set; }
public string Email { get; set; }
public DateTime ts { get; set; }
public string Username { get; set; }
public string Password { get; set; }
}
}
我的控制器
[HttpPost]
public JsonResult Login(UsersModel u)
{
if (doSomething(Prop1, Prop2)
return Json(null); // Success
return Json(new { Status:'success',Msg:" Your username"+u.username });
}
我的控制器不获取数据。调试时,它得到空。
My controller does not get the data. It gets null when debugging.
我在做什么错了?
注意
我删除在脚本文件夹所有的JavaScript,因为我使用的是自定义脚本
I deleted all the javascript in the Scripts folder, as I am using a custom script
推荐答案
许多调试后,我发现罪魁祸首
After much debugging, i found the culprit
在我的AJAX功能之上,我有这个两行
in my ajax function above i have this two line
contentType: 'application/json; charset=utf-8',
dataType:'json',
不知何故,这不应该是。当我提出的两行present的形式,这是我在Chrome浏览器开发看了
Somehow, this should not be. when i submitted the form with the two lines present, this is what i saw in chrome dev
但是,当我删除行的contentType:应用/ JSON的;字符集= UTF-8
,使$阿贾克斯现在是
But when i removed the line contentType: 'application/json; charset=utf-8'
, so that the $.ajax is now
$.ajax({ // create an AJAX call...
type: 'post', // GET or POST
url: url, // the file to call
dataType:'json',
data: data, // get the form data
Success:successFunction, Error: ErrorFunction
});
当我在Chrome浏览器开发看后提交,我看到这一点,这是以前没有的。
when i look in chrome dev after submit, i see this, which was not there before
我不'知道为什么是这样的话,我会尽量在其他浏览器,看是否所有的作品
I dont' know why this is the case, i will try in other browsers to see if it all works
这篇关于使用jQuery后张贴到Asp.net 4 MVC 2的空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!