Error with setRequestHeader in GET request

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 14:52:37

问题


The following JS function should be sending out a GET request to

http://127.0.0.1:5000/api.xml

with

?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);
}

Instead, it always queries

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

ignoring the fact that I required a GET on

http://127.0.0.1:5000/api.xml


回答1:


1)As Stephan Schrijver lined out

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

is only valid when it follows

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

as part of a POST request

It is no longer required for GET requests

2) Also,

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

must be defined before the readyStateChange function




回答2:


Try this out

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);
}



回答3:


At first, you should create desired URL, open request and then set request header. Let me show an example:

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();

Or in your case:

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();


来源:https://stackoverflow.com/questions/56374246/error-with-setrequestheader-in-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!