当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - loading a location using post method

问题描述:

Normally we use

window.location.href="/index.php?querystring";

in javascript. Is there a way to send the querystring over post method without having any form in document?

网友答案:

You need to use XMLHttpRequest to do this.

Demo: http://jsfiddle.net/ThinkingStiff/bCnuE/4/

Script:

function post( url, data, success, error ) {

    var ajax = new window.XMLHttpRequest();

    ajax.onreadystatechange = function () {
        if ( ajax.readyState == 4 ) { //response ready

            if ( ajax.status == 200 ) { //success
                if ( success ) success( ajax.responseText, ajax.statusText );
            } else {
                if ( error ) error( ajax.statusText );
            };
        };
    };

    ajax.open( 'POST', url );
    ajax.send( data );

};

post( '/index.php', 'querystring', 
    function ( response, status ) {
        //success'
    },
    function ( error ) {
        //error'
    } 
);
分享给朋友:
您可能感兴趣的文章:
随机阅读: