【问题标题】:REST API Post body from XML endpoint URL来自 XML 端点 URL 的 REST API 发布正文
【发布时间】:2022-01-25 15:28:25
【问题描述】:

我正在尝试向服务器 (https://exampleapi.com/echo/post/xml) 发出发布请求,并且我有一段可以工作的代码。我想要实现的是从 URL https://randomuser.me/api/?format=xml 加载 XML,而不是反引号中包含的 XML。 以下是我的代码示例:

var url = "https://exampleapi.com/echo/post/xml";

var xhr = new XMLHttpRequest();
xhr.open("POST", url);

xhr.setRequestHeader("Content-Type", "application/xml");
xhr.setRequestHeader("Accept", "application/xml");

xhr.onreadystatechange = function () {
  if (xhr.readyState === 4) {
    console.log(xhr.status);
    console.log(xhr.responseText);
  }
};

var data = `<user>
<results>
<gender>male</gender>
<name>
<title>Mr</title>
<first>Jasper</first>
<last>Smith</last>
</name>
<location>
<street>
<number>1966</number>
<name>Chatham Road</name>
</street>
<city>Dunedin</city>
<state>Canterbury</state>
<country>New Zealand</country>
<postcode>63789</postcode>
<coordinates>
<latitude>66.6618</latitude>
<longitude>-110.4640</longitude>
</coordinates>
<timezone>
<offset>-10:00</offset>
<description>Hawaii</description>
</timezone>
</location>
<email>jasper.smith@example.com</email>
<login>
<uuid>8a132229-407b-47db-9a03-73c5d6c8c969</uuid>
<username>angryelephant526</username>
<password>nicole1</password>
<salt>saEREYSt</salt>
<md5>4292d6c17f10dad0224746c16f510032</md5>
<sha1>43d82fdd05fe62edcec774d534aaa41dde6c32dd</sha1>
<sha256>891f6c6f1193b59d70fd61c482575c9c04fa951123ec081e459b2a46235e1694</sha256>
</login>
<dob>
<date>1955-01-13T12:46:36.403Z</date>
<age>66</age>
</dob>
<registered>
<date>2015-08-05T20:56:20.380Z</date>
<age>6</age>
</registered>
<phone>(847)-575-4692</phone>
<cell>(051)-964-8266</cell>
<id>
<name/>
<value/>
</id>
<picture>
<large>https://randomuser.me/api/portraits/men/34.jpg</large>
<medium>https://randomuser.me/api/portraits/med/men/34.jpg</medium>
<thumbnail>https://randomuser.me/api/portraits/thumb/men/34.jpg</thumbnail>
</picture>
<nat>NZ</nat>
</results>
<info>
<seed>9dca58235eff2b2b</seed>
<results>1</results>
<page>1</page>
<version>1.3</version>
</info>
</user>`;

xhr.send(data);

【问题讨论】:

  • 这能回答你的问题吗? How to get the response of XMLHttpRequest?
  • 您可以发出多个 xhr 请求。只需请求第一个,然后使用第一个中的数据制作第二个。
  • @skara9 谢谢,我已经尝试过了,看来我可能遗漏了一些东西。能否请您出示您的想法的代码 sn-p?

标签: javascript node.js xml


【解决方案1】:

您可以一个接一个地发出多个 AJAX 请求。

这是一个示例,其中xhr1 向第一个 url 发出 GET 请求,xhr2 使用来自xhr1 的数据向第二个 url 发出 POST 请求。

const xhr1 = new XMLHttpRequest();
xhr1.open('GET', "https://randomuser.me/api/?format=xml");
xhr1.setRequestHeader("Accept", "application/xml");

xhr1.onload = function() {
  xhr2.send(xhr1.responseXML); // get the data from xhr1 and send in xhr2
}

const xhr2 = new XMLHttpRequest();
xhr2.open('POST', "https://exampleapi.com/echo/post/xml");
xhr2.setRequestHeader("Content-Type", "application/xml");
xhr2.setRequestHeader("Accept", "application/xml");

xhr2.onload = function() {
  console.log(xhr2.responseXML); // get the data from xhr2 and log it
}

xhr1.send();

您也可以使用fetch:

fetch("https://randomuser.me/api/?format=xml", { method: "GET", headers: { 'Accept': 'text/xml' } }).then(res => res.text()).then(data => {
  fetch("https://exampleapi.com/echo/post/xml", { method: "POST", headers: { 'Content-Type': 'text/xml', 'Accept': 'text/xml' }, body: data }).then(res => res.text()).then(console.log);
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-24
    • 2023-03-18
    • 1970-01-01
    • 2019-04-07
    • 2014-01-15
    • 1970-01-01
    • 2021-08-08
    • 2015-06-19
    相关资源
    最近更新 更多