使用post方法加载位置

时间:2012-06-28 06:21:11

标签: javascript ajax

通常我们使用

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

在javascript中。有没有办法在post方法上发送查询字符串而不在文档中有任何形式?

1 个答案:

答案 0 :(得分:0)

您需要使用XMLHttpRequest来执行此操作。

演示:http://jsfiddle.net/ThinkingStiff/bCnuE/4/

脚本:

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'
    } 
);