经典ASP / Javascript-原始JSON POST API调用

时间:2018-07-04 21:13:49

标签: javascript json ajax asp-classic xmlhttprequest

我必须通过经典的ASP / JavaScript页面调用API。因此,在我的包含项中,我尝试调用了以下函数,但它返回错误:

    function returnFunction(d) {
        try {
            var urlstring = "https://website.com/apidir"

          var sentdata = "Var1=" + d.var1
                + "&Var2=" + d.var2
                + "&Var3=" + d.var3
                + "&Var4=false&Var5=false&Var6=false";

            var HttpReq = Server.CreateObject("Msxml2.ServerXMLHTTP");
            HttpReq.open("POST", urlstring, false);
            HttpReq.setRequestHeader("Content-Type", "application/json; charset=utf8");
            HttpReq.send(sentdata);
            var response = HttpReq.responseText;

            var data = JSON.parse(response);

            return (data.status + "").toLowerCase();
        }
        catch (e) {
        }
    }

并且还尝试用

替换senddata
{"Var1": "1001","Var2": "102455","Var3": "1880","Var4": "false","Var5": "false","Var6": "false"}

注意:我在Postman中尝试了该呼叫,并得到以下响应:

  

{     “状态”:{       “ StatusNumber”:“ 10”,       “ StatusMessage”:“没有记录的标准”     },     “ GetData”:{       “ Name1”:空,       “ Name2”:空     }   }

所以上述也是我应该得到的回报,但是在执行网络通话时却遇到了以下错误

  

{“状态”:{“代码”:“ 19”,“消息”:“失败”}}

我还尝试了以下带有一些硬编码参数的代码,它给了我与上面相同的错误:

function returnFunction(d) {
    try {
        var HttpReq = Server.CreateObject("Msxml2.ServerXMLHTTP");
        var urlstring = "https://website.com/apidir";
        HttpReq.open("POST", urlstring, false);
        HttpReq.setRequestHeader("Content-type", "application/json; charset=utf8");
        var parameters = JSON.stringify({"Var1": "1001","Var2": "102455","Var3": "1880","Var4": "false","Var5": "false","Var6": "false"});

        HttpReq.send(parameters);

        var response = HttpReq.responseText;

        var data = JSON.parse(response);

        return (response + "").toLowerCase();          
    }
    catch (e) {
    }
}

注意,我在经典的pas页面中使用以下名称进行调用:

var valResponse = returnFunction({ var1: var1, var2: var2 });

然后是通常的:

<%=valResponse %>

感谢您查看此内容和帮助

0 个答案:

没有答案