【问题标题】:Error on sending POST request发送 POST 请求时出错
【发布时间】:2015-07-07 00:33:31
【问题描述】:

我无法弄清楚为什么我的代码不起作用。我要做的是向一个在线 API 发送一个 POST 请求,该 API 也会返回 JSON 数据。在它说“正在搜索帐户数据...”之后,该代码似乎没有执行很多其他操作。

函数请求板信息(){

var xmlhttp = new XMLHttpRequest();
//BoardInfoUrl = getBoardInfoUrl();
BoardInfoURL = "https://meshech.leankit.com/kanban/api/board/2847596743/searchcards";
        var parameters = {
            "searchOptions":{
                "SearchTerm": "",
                "SearchInBoard": true,
            }
        };

xmlhttp.open("POST", BoardInfoUrl, true);

xmlhttp.setRequestHeader("Content-type", "application/json");
document.getElementById("search").innerHTML = "Searching for account data...";

xmlhttp.onreadystatechange = function () {
    document.getElementById("search").innerHTML = "Processing next request...";

    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        //define what to retrieve here
        document.getElementById("search").innerHTML = "jsonData found!";
        jsonData = xmlhttp.responseText;
        createFile();//creates a text file

        //the variable below is the parameters for the POST function in JSON.

    }
}
xmlhttp.send(JSON.stringify(parameters));

}

我确定它没有进入 onreadystatechange 函数,但我不知道为什么。任何帮助将不胜感激。 提前致谢。

编辑:好的,所以我将代码更改为在 setrequestheader 之前打开。但现在它在打开时停止。

【问题讨论】:

  • 浏览器的调试控制台说什么?

标签: javascript json post


【解决方案1】:

从此错误:

Uncaught InvalidStateError: Failed to execute 'setRequestHeader' on 'XMLHttpRequest':
The object's state must be OPENED.

我会说你需要先执行.open(),然后再执行.setRequestHeader()

https://jsfiddle.net/q6w12jae/

【讨论】:

  • BoardInfoUrl 和 BoardInfoURL 不一样。我让它工作。感谢您的帮助。
【解决方案2】:

BoardInfoURL 未定义,超出范围(请参阅开头的 var):

var BoardInfoURL = "https://meshech.leankit.com/kanban/api/board/2847596743/searchcards";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 2015-01-25
    • 2022-08-03
    • 1970-01-01
    相关资源
    最近更新 更多