我正在使用 XMLHttpRequest 使用 JavaScript 發出發布請求。我提出請求并使用send()帶有 x-www-form-urlencoded 字串作為引數的方法。但是當在瀏覽器中發出這個請求時,我傳遞的引數不會隨請求一起發送。
示例:我發送一個帖子請求
xhttp = new XMLHttpRequest();
xhttp.onload = function() {
//code
}
xhttp.open('POST', 'https://example.com/request', true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send('lorem=ipsum&title=title');
所以請求被發送,而不是像https://example.com/request?lorem=ipsum&title=title
這樣發送:它像這樣發送它:https://example.com/request沒有任何引數。
uj5u.com熱心網友回復:
的xhttp.send()引數是請求的正文,而不是請求的引數。所以如果后端需要引數,腳本不會發送任何引數。移動lorem=ipsum&title=title到xhttp.open('POST', 'https://example.com/request?lorem=ipsum&title=title', true);
應該作業
xhttp = new XMLHttpRequest();
xhttp.onload = function() {
//code
}
xhttp.open('POST', 'https://example.com/request?lorem=ipsum&title=title', true);
xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhttp.send();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/495946.html
