简体   繁体   中英

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?

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. Don't try generating JSON by hand, use an JSON library.

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

You are calling a() instead of assigning a as the success handler. Remove the () if you want to assign the function instead of its return value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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