Ajax 调用不起作用

Ajax call does not work

本文关键字:不起作用 调用 Ajax      更新时间:2023-09-26
$('#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";
            }
        });

由于我已经接受了答案,它转到服务器端代码验证用户并控制传递到"a"功能,但它不显示登录.aspx页面...有什么线索吗?

应该是

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

您正在生成的 JSON 无效。不要尝试手动生成 JSON,请使用 JSON 库。

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

您正在调用a(),而不是将a分配为成功处理程序。如果要分配函数而不是其返回值,请删除()