邮政服务有一个API,可让您发送包含包裹重量,旅行信息等的xml请求。它将返回xml响应。
如何处理xml响应?我或者需要在客户端解析xml,或者更可取的是,将xml放入一个可以发送到laravel后端进行解析的变量中。
顺便说一句,我正在使用react和laravel。
getPostagePrice = () => {
fetch('http://production.shippingapis.com/ShippingApi.dll?API=RateV4&XML=<RateV4Request USERID="XXXXXXXXXXX"><PackageID="1ST"><Service>PRIORITY</Service><ZipOrigination>44106</ZipOrigination><ZipDestination>20770</ZipDestination><Pounds>1</Pounds><Ounces>8</Ounces><Container>NONRECTANGULAR</Container><Size>LARGE</Size><Width>15</Width><Length>30</Length><Height>15</Height><Girth>55</Girth></Package></RateV4Request>', {
method: 'get',
}).then((response) => {
console.log(response.text());
}).then(str => (new window.DOMParser()).parseFromString(str, "text/xml")
).then(data => console.log(data));
}
Run Code Online (Sandbox Code Playgroud)