0
的位置通常我們在JavaScript中使用加載使用POST方法
window.location.href="/index.php?querystring";
。有沒有辦法發送querystring而不是在文檔中有任何形式的post方法?
的位置通常我們在JavaScript中使用加載使用POST方法
window.location.href="/index.php?querystring";
。有沒有辦法發送querystring而不是在文檔中有任何形式的post方法?
您需要使用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'
}
);