我有登录表单,用户可以写用户名和密码,我检查用户名是否正确我试图通过ajax将我的控制器代码发送给家庭控制器
[HttpPost]
public JsonResult ValidateUser(string username,string password)
{
using(var contxt=new EnglisCenterEntities())
{
var data = from a in contxt.Employee
where a.Username == username && a.Passwords == password
select a.EmpName;
string Empname = data.FirstOrDefault();
if(data.Count()>0)
{
return Json(new { Success = true } ,JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { Success = false }, JsonRequestBehavior.AllowGet);
}
}
}
这是我的ajax代码
<script>
$(document).ready(function () {
$('#savedata').click(function (e) {
e.preventDefault();
var data = {
username: function () { return $("#username").val(); },
password: function () { return $("#password").val(); }
};
$.ajax({
url: "/Account/validateuser",
type: "POST",
data: data,
dataType: "json",
success: function (response) {
window.location.href = "@Url.Action("Index", "Home")";
}
else
notifyBar();
@*window.location.href = "@Url.Action("Login", "Account")";*@
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});
此代码检查用户是否存在并且它正在工作,但我想通过重定向索引操作来发送用户名。我怎么能这样做?
答案 0 :(得分:1)
您可以使用此代码
var xsrf = 'username=' + username + '&password='+password;
$.ajax({
url: "/Account/validateuser",
type: "POST",
data: xsrf ,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }...
`
答案 1 :(得分:0)
在Url.Action
中,您可以发送其他数据:
@Url.Action("Index", "Home", new { Name = "yourUserName" })
答案 2 :(得分:0)
确保通过Ajax(密钥,值)对发送数据。那些键是相同的(区分大小写)
听到我显示,您发送密钥:用户名和您的控制器接收:用户名
$(document).ready(function () {
$('#savedata').click(function (e) {
e.preventDefault();
var data = {
username: function () { return $("#username").val(); },
password: function () { return $("#password").val(); }
};
$.ajax({
url: "/Account/validateuser",
type: "POST",
data: data,
dataType: "json",
success: function (response) {
window.location.href = "@Url.Action("Index", "Home")";
}
else
notifyBar();
@*window.location.href = "@Url.Action("Login", "Account")";*@
},
error: function () {
console.log('Login Fail!!!');
}
});
});
});