问题描述:

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'
    } 
);
相关阅读:
Top