开发者

How do I make an asynchronous HTTP request in Javascript?

How do I asynchronously send a HTTP request with POST method and some variables to a page, like it was opened in a browser?

I integrated my login system with my forum's, but, to complete the login, the user must be redirected to the forum and then back to the current page. This is a kind of killer for my iPhone web app.

With this request I need to send some POST variables so the login can actually take place. How do I do this in JavaScript and/or jQuery?

Edit: According to the jQuery AJAX library examples, this should work:

$.ajax({
    type: "POST",
    url: "/forum/ucp?mode=login",
    data:
    ({
        username : $("#username").first().val(),
        password : $("#password").first().开发者_StackOverflow中文版val(),
        autologin : $("#autologin").first().val(),
        viewonline : $("#viewonline").first().val(),
        redirect : window.location
    }),
    success: function(msg)
    {
        location.reload();
    }
});

But guess what? It doesn't!


    $.ajax({
        type: "POST",
        url: "login.php",
        data: "user=bob&password=pass",
        success: function(data) {
            //function called when the server responds
        }
    }


Just use an XMLHttpRequest object. This object is native to JavaScript. There is plenty of instructions for this class on the web.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜