简体   繁体   English

Ajax发布参数ASP.NET MVC 3

[英]Ajax post parameters ASP.NET MVC 3

Hello guys i have the next ajax call for login. 大家好,我有下一个ajax电话需要登录。 I serialize the form and send the data to server and return redirect url link. 我对表格进行序列化,然后将数据发送到服务器并返回重定向URL链接。 My problem is that my url after post is like 我的问题是发布后的网址就像

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

It looks like you wrote this $.ajax call in the .click event of a submit button or in the .submit event of a form without canceling the default action by returning false from the callback or by calling preventDefault on the argument. 看起来您在提交按钮的.submit事件或表单的.submit事件中编写了$.ajax调用, .click没有通过从回调返回false或对参数调用preventDefault来取消默认操作。 Here's how your code should look like: 您的代码应如下所示:

$('#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
});

Also async: false, ????? async: false, ????? You know what this does, do you? 您知道这是什么,对吗? That's not AJAX. 那不是AJAX。 That's a blocking synchronous call to your webserver during which the client browser would be frozen like during the Ice Age 2 ruining all user experience. 这是对您的网络服务器的阻塞同步调用,在此期间客户端浏览器将被冻结,就像在《冰河世纪2》中摧毁所有用户体验一样。

Try returning false at the end of your submit function 尝试在您的Submit函数结尾处返回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; });

Another option would of course be to return the correct redirectlink from the controller instead of overriding it in the java script. 当然,另一个选择是从控制器返回正确的重定向链接,而不是在Java脚本中覆盖它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM