亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用XMLHttpRequest發送POST數據

使用XMLHttpRequest發送POST數據

使用XMLHttpRequest發送POST數據我想在JavaScript中使用XMLHttpRequest發送一些數據。假設我在HTML中有以下表單:<form name="inputform" action="somewhere" method="post">     <input type="hidden" value="person" name="user">     <input type="hidden" value="password" name="pwd">     <input type="hidden" value="place" name="organization">     <input type="hidden" value="key" name="requiredkey"></form>如何在JavaScript中使用XMLHttpRequest編寫等效代碼?
查看完整描述

3 回答

?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

下面的代碼演示了如何執行此操作。


var http = new XMLHttpRequest();

var url = 'get_data.php';

var params = 'orem=ipsum&name=binny';

http.open('POST', url, true);


//Send the proper header information along with the request

http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');


http.onreadystatechange = function() {//Call a function when the state changes.

    if(http.readyState == 4 && http.status == 200) {

        alert(http.responseText);

    }

}

http.send(params);


查看完整回答
反對 回復 2019-05-29
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

var xhr = new XMLHttpRequest();

xhr.open('POST', 'somewhere', true);

xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

xhr.onload = function () {

    // do something to response

    console.log(this.responseText);

};

xhr.send('user=person&pwd=password&organization=place&requiredkey=key');

或者,如果您可以依賴瀏覽器支持,則可以使用FormData


var data = new FormData();

data.append('user', 'person');

data.append('pwd', 'password');

data.append('organization', 'place');

data.append('requiredkey', 'key');


var xhr = new XMLHttpRequest();

xhr.open('POST', 'somewhere', true);

xhr.onload = function () {

    // do something to response

    console.log(this.responseText);

};

xhr.send(data);


查看完整回答
反對 回復 2019-05-29
  • 3 回答
  • 0 關注
  • 9052 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號