Ajax调用不起作用

时间:2013-08-06 13:39:59

标签: javascript jquery asp.net ajax

$('#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”函数,但它不显示login.aspx页面...任何线索?

2 个答案:

答案 0 :(得分:3)

应该是

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

答案 1 :(得分:2)

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

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

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