Ajax post参数ASP.NET MVC 3
大家好,我有下一个ajax登录调用。我序列化表单并将数据发送到服务器并返回重定向url链接。我的问题是,我发布后的url是Ajax post参数ASP.NET MVC 3,ajax,asp.net-mvc,asp.net-mvc-3,jquery,asp.net-ajax,Ajax,Asp.net Mvc,Asp.net Mvc 3,Jquery,Asp.net Ajax,大家好,我有下一个ajax登录调用。我序列化表单并将数据发送到服务器并返回重定向url链接。我的问题是,我发布后的url是 http://localhost:50802/?username=&password= and not http://localhost:50802/Home $.ajax({ type: "POST", url: "Login/Login", dataType: 'j
http://localhost:50802/?username=&password= and not http://localhost:50802/Home
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
alert(result.link);
window.location.replace = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
这看起来像是您在
中编写的$.ajax
调用。单击submit按钮的事件或表单的.submit
事件,通过回调返回false或对参数调用preventDefault
来取消默认操作。下面是您的代码的外观:
$('#id_of_your_form').submit(function(e) {
e.preventdefault(); // <-- That's what I am talking about and what you forgot
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
window.location.replace = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
});
$(“#id_of_your_form”)。提交(函数(e){
e、 preventdefault();//尝试在提交函数结束时返回false
$('#id_of_your_form').submit(function(e) {
$.ajax({
type: "POST",
url: "Login/Login",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: loginJson,
cache: true,
async: false,
complete: function (result) {
window.location = "/Home/Index";
},
error: function () {
$("#username").val("");
$("#password").val("");
alert("Wrong Username or Password!");
}
}); //end ajax call
return false; });
当然,另一种选择是从控制器返回正确的重定向链接,而不是在java脚本中重写它。I put e.preventdefault()它不能解决问题参数仍然存在,我无法使重定向工作。有趣的是,当我在firebug中使用debug时,它会在我通过ajax post XD后重定向。也许你有一些javascript错误?你能在控制台中看到它们吗?我在控制台中看不到任何一个。我能看到的是,它确实会提醒我success函数,但它不会像success那样更改url:function(result){alert(result.link);window.location.href=“/Home/Index”},当我调试它时,它会触发success函数并重定向工作。