Error with setRequestHeader in GET request

2019-08-28 03:12发布

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

3条回答
仙女界的扛把子
2楼-- · 2019-08-28 03:25

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

查看更多
甜甜的少女心
3楼-- · 2019-08-28 03:41

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);
}
查看更多
Emotional °昔
4楼-- · 2019-08-28 03:42

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();
查看更多
登录 后发表回答