简体   繁体   English

Ajax调用不起作用

[英]Ajax call does not work

$('#loginbtn').click(function() {
            var userName = document.getElementById('uid').value;
            var password = document.getElementById('pwd').value;

            $.ajax({
                type: "POST",
                url: "/LoginNew.aspx/Authenticate",
                data: "{ 'userName': '" + userName + "' ,'password': '" + password + "' }",
                async: false;
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: a(),
                error: function(e) {
                    alert(e.valueOf());
                }
            });

         alert("test");
            function a() {
                window.location.href = "Login.aspx";
            }
        });

As I have accepted the answer its goes to the server side code authenticates user and control passes to "a" function but it does not display login.aspx page...Any clues? 当我接受答案时,它进入服务器端代码,对用户进行身份验证,并将控制权传递给“ a”功能,但它不显示login.aspx页...有任何提示吗?

it should be 它应该是

$('#loginbtn').click(function() {
    var userName = document.getElementById('uid').value;
    var password = document.getElementById('pwd').value;

    $.ajax({
        type : "POST",
        url : "/LoginNew.aspx/Authenticate",
        data : { 
            userName: userName ,
            password: password 
        },
        async : false, // not ; need to use ,
        contentType : "application/json; charset=utf-8",
        dataType : "json",
        success : a, // pass the callback reference, don't invoke it
        error : function(e) {
            alert(e.valueOf());
        }
    });

    alert("test");
    function a() {
        window.location.href = "Login.aspx";
    }
});

The JSON you are generating is invalid. 您生成的JSON无效。 Don't try generating JSON by hand, use an JSON library. 不要尝试手动生成JSON,请使用JSON库。

JSON.stringify({ userName: userName, password: password })

You are calling a() instead of assigning a as the success handler. 您正在调用 a()而不是将a分配为成功处理程序。 Remove the () if you want to assign the function instead of its return value. 如果要分配函数而不是返回值,请删除()

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

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