以下 JS 函数应向
发送 GET 请求与
?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);
}
相反,它总是查询
忽略我需要 GET 的事实
最佳答案
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/