GET请求中的setRequestHeader错误 [英] Error with setRequestHeader in GET request

查看:201
本文介绍了GET请求中的setRequestHeader错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下JS函数应该向

http://127.0.0.1:5000/api.xml

?query = toast

?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的事实

ignoring the fact that I required a GET on

http://127.0.0.1:5000/api.xml

推荐答案

1)Stephan Schrijver排队

1)As Stephan Schrijver lined out

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

仅在遵循

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

作为 POST请求

它是不再需要GET请求

2)另外

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

必须在readyStateChange函数之前定义

must be defined before the readyStateChange function

这篇关于GET请求中的setRequestHeader错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆