gpt4 book ai didi

javascript - GET 请求中 setRequestHeader 错误

转载 作者:行者123 更新时间:2023-12-02 23:33:50 26 4
gpt4 key购买 nike

以下 JS 函数应向

发送 GET 请求

http://127.0.0.1:5000/api.xml

?query=toast

function sendRequest(str){
var request = new XMLHttpRequest();
console.log('sending request');
request.onreadystatechange = function() {
if (request.readyState == XMLHttpRequest.DONE) {
json=request.responseText;
//json.forEach(function(obj) {
//});
for (word in json){
var row=table.insertRow();
var scoreC=row.insertCell();
var wordC=row.insertCell();
scoreC.innerHTML=json[word];
wordC.innerHTML=word;
}
} else {
concole.log("Silence on the line");
}
}
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.open('GET', 'http://127.0.0.1:5000/api.xml?query='+str, true);
request.send();
// and give it some content
//var newContent = document.createTextNode(resp);
//console.log(resp.responseType);
}

相反,它总是查询

http://127.0.0.1:5000/?word=toast

忽略我需要 GET 的事实

http://127.0.0.1:5000/api.xml

最佳答案

1) 正如 Stephan Schrijver 所言

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

只有跟在后面时才有效

request.open('POST', 'http://127.0.0.1:5000/api.xml?query='+str, true);

作为POST 请求的一部分

no longer required for GET requests

2)另外,

request.open('GET', 'http://127.0.0.1:5000/api.html?query='+str, true);

必须在readyStateChange函数之前定义

关于javascript - GET 请求中 setRequestHeader 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56374246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com