GET请求中的setRequestHeader错误

时间:2019-05-30 08:12:23

标签: javascript html onclick

以下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

3 个答案:

答案 0 :(得分:1)

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函数之前定义

答案 1 :(得分:0)

尝试一下

function sendRequest(str) {
    const request = new XMLHttpRequest();
    request.onreadystatechange = function() {
        if (this.readyState === 4) {
            if (this.status === 200) {
                console.log(this.responseText);
                const json = this.responseText;
                for (word in json) {
                    var row = table.insertRow();
                    var scoreC = row.insertCell();
                    var wordC = row.insertCell();
                    scoreC.innerHTML = json[word];
                    wordC.innerHTML = word;
                }
            } else if (this.response == null && this.status === 0) {
                console.log(this.responseText);
            } else {
                console.log('Error');
            }
        }
    };
    request.open('GET', 'http://127.0.0.1:5000/api.xml?query=' + str, true);
    request.send(null);
}

答案 2 :(得分:0)

首先,您应该创建所需的URL,打开请求,然后设置请求标头。让我举个例子:

function sendRequest(){
  let theUrl = 'http://127.0.0.1:5000/api.xml'

  let xmlHttp = new XMLHttpRequest();
  let fooStr='?query=toast';
  theUrl = `http://127.0.0.1:5000/api.xml${fooStr}`;
  xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
  xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

  xmlHttp.send( null );
  return xmlHttp.responseText;
}

sendRequest();

或者您的情况:

request.open('GET', 'http://127.0.0.1:5000/api.xml?query='+str, true);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.send();