簡體   English   中英

發送POST請求時出錯

[英]Error on sending POST request

我在弄清楚為什么我的代碼無法正常工作時遇到了麻煩。 我想做的是將POST請求發送到在線API,該API也將返回JSON數據。 在顯示“正在搜索帳戶數據...”之后,該代碼似乎沒有執行任何其他操作。

函數RequestBoardInfo(){

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之前打開。 但現在它停止開放了。

從這個錯誤:

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

我說你需要先執行.open() ,然后執行.setRequestHeader()

https://jsfiddle.net/q6w12jae/

BoardInfoURL ,超出范圍(請參閱開頭的var ):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM