简体   繁体   English

使用 XML 获取 HTTP 请求

[英]Fetch HTTP request using XML

I'm trying to make a request to this API which works fine when submitting in the browser with my XML data but I can't see to get it to work with the fetch API. I get STATUS 200 OK but no data.我正在尝试向这个 API 发出请求,当在浏览器中提交我的 XML 数据时,它工作正常,但我看不到它与提取 API 一起工作。我得到 STATUS 200 OK 但没有数据。 I'm pretty sure I'm not submitting the body correctly but can't work out why?我很确定我没有正确提交正文,但无法弄清楚为什么?

Checking the console when submitted directly in the browser vs my application I can see the data is submitted with 'params' which I seem to be missing?直接在浏览器中提交时检查控制台与我的应用程序相比,我可以看到数据是用我似乎丢失的“参数”提交的?

Appreciate any guidance.感谢任何指导。

my response correct response我的反应正确的反应

const xml =
    '<GetOrderStatus><ClientID>FITE****</ClientID><UserID>FI****</UserID><Password>*****</Password><SecurityKey>*****/SecurityKey><Order><OrderNum>1834006076</OrderNum></Order></GetOrderStatus>';

fetch('http://api.3linx.com/v4/getorderstatus', {
    method: 'POST',
    mode: 'no-cors',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Accept:
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'Accept-Language': 'en-GB',
        'Accept-Encoding': 'gzip, deflate',
        Connection: 'Keep-alive',
    },
    body: xml,
    
})
    .then((response) => response.text())
    .then((text) => console.log(text));

Since you use application/x-www-form-urlencoded , you'll have to use URLSearchParams to send your data with params: .由于您使用application/x-www-form-urlencoded ,因此您必须使用URLSearchParams来发送带有params:

 const xml = '<GetOrderStatus><ClientID>FITE****</ClientID><UserID>FI****</UserID><Password>*****</Password><SecurityKey>*****/SecurityKey><Order><OrderNum>1834006076</OrderNum></Order></GetOrderStatus>'; const form = new URLSearchParams(); form.append('params', xml); fetch('http://api.3linx.com/v4/getorderstatus', { method: 'POST', mode: 'no-cors', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language': 'en-GB', 'Accept-Encoding': 'gzip, deflate', Connection: 'Keep-alive', }, body: form, }).then((response) => response.text()).then((text) => console.log(text));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM