如何通过javascript将xml文件发送到api

mepcadol  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(471)

我今天向您报告,并礼貌地通知我已收到指示,当我尝试发送api请求时,我会这么做。是的,我知道,在网上提供关于js/ajax/jquery的信息是有权利的,尽管它通常涉及到读取api,所以它只适用于一个api函数。代码如下:
当然,我是故意更改api密钥的,所以这不是问题,密钥本身很好

<script>
UserAction () {
    var xhr = new XMLHttpRequest ();
    xhr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
           // Common Action To Take When Document is ready:
            var answer = xhr.responseText;
            console.log ("ok" + response);
        }
    };
    xhr.open ('POST', 'https: //api5.esv2.com/v2/Api/Subscribers/', true);

    xhr.send ("POST https://api5.esv2.com/v2/Api/Subscribers/ HTTP / 1.1 Content-Type: text / xml <ApiRequest xmlns: xsi = \" http://www.w3.org/ 2001 / XMLSchema-instance \ "xmlns: xs = \" http: //www.w3.org/2001/XMLSchema \ "> <ApiKey> KEY </ApiKey> <xsi data: type = \" Subscriber \ "> < Mode> AddAndUpdate </Mode> <Force> true </Force> <ListId> 1 </ListId> <Email> jan.kowalski@domena.com </Email> <Firstname> Jan </Firstname> <Lastname> Kowalski < /Lastname><TrackingCode>123</TrackingCode><Vendor>xyz</Vendor><Ip>11.22.33.44</Ip><Properties><Property><Id>5</Id><Value xsi: type = \ "xs: string \"> student </Value> </Property> <Property> <Id> 3 </Id> <Value xsi: type = \ "xs: dateTime \"> 1985-03-12 </Value> </Property></Properties></Data> </ApiRequest> ");

}
lmvvr0a8

lmvvr0a81#

您需要正确设置标题,并且仅在中使用xml send .

xhr.open ('POST', 'https://api5.esv2.com/v2/Api/Subscribers/', true);
xhr.setRequestHeader("Content-Type", "text/xml");
xhr.send(XMLbody);

根据情况(您期望的响应类型),您可能也需要更改此部分。

var answer = xhr.responseXML;

相关问题