【问题标题】:How to send an XML file to API by javascript如何通过 javascript 将 XML 文件发送到 API
【发布时间】:2021-09-19 18:21:06
【问题描述】:

我今天向您报告,并礼貌地通知我被指示,我在尝试发送 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> ");

}

【问题讨论】:

    标签: javascript api rest post


    【解决方案1】:

    您需要正确设置标题并仅在send 中使用您的 XML。

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

    根据情况(您期望得到什么样的响应),您可能也需要更改这部分。

    var answer = xhr.responseXML;
    

    【讨论】:

    • 不幸的是,仅添加代码的第一部分并没有帮助。你能向我解释一下如何更换这部分吗? var answer = xhr.responseXML;
    • 您是否只将 XML 传递给您的 xhr.send ?不包括 POST、Content-Type 和其他垃圾?
    猜你喜欢
    • 1970-01-01
    • 2020-10-12
    • 2019-03-09
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2014-05-10
    • 2023-03-16
    相关资源
    最近更新 更多