发送POST请求时出错

发送POST请求时出错

本文介绍了发送POST请求时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚为什么我的代码无法正常工作时遇到了麻烦.我想做的是向在线API发送POST请求,该请求还将返回JSON数据.在显示正在搜索帐户数据..."之后,该代码似乎并没有执行任何其他操作.

I am having trouble figuring out why my code is not working. What I am tying to do is send a POST request to an online API that will also return JSON data. after it says, "Searching for account data..." the code does not seem to execute much of anything else.

function RequestBoardInfo(){

function 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函数,但是我迷失了为什么.任何帮助将不胜感激.预先感谢.

I know for sure that it's not entering the onreadystatechange function but i'm at a lost for why. Any help will be greatly appreciated.Thanks in advance.

确定,所以我将代码稍稍更改为在setrequestheader之前打开.但现在它停止在打开状态.

ok so I changed the code a bit to open before setrequestheader. but now it stops at open.

推荐答案

出现此错误:

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

我想说的是,您需要先执行.open(),然后再执行.setRequestHeader()

I'd say you need to perform .open() first, and then .setRequestHeader()

https://jsfiddle.net/q6w12jae/

这篇关于发送POST请求时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 22:43