gpt4 book ai didi

angular - Canada Post Rates Error in Ionic Project Response for preflight has invalid HTTP status code 500

转载 作者:太空狗 更新时间:2023-10-29 17:36:39 26 4
gpt4 key购买 nike

我正在处理一个 HTTP 请求,该请求向加拿大邮政 API 发送 POST 请求以查询运费报价:

getRates(weight: number, originPostal, destPostal) {
const options = {
headers: new HttpHeaders({
'Authorization': 'Basic ' + btoa(this.TEST_USERNAME + ':' + this.TEST_PASSWORD),
'Accept': 'application/vnd.cpc.ship.rate-v3+xml',
'Content-Type': 'application/vnd.cpc.ship.rate-v3+xml',
'Accept-language': 'en-CA',
}),
};

const body = `
<?xml version="1.0" encoding="utf-8"?>
<mailing-scenario xmlns="http://www.canadapost.ca/ws/ship/rate-v3">
<customer-number>${this.TEST_NUMBER}</customer-number>
<parcel-characteristics>
<weight>${weight}</weight>
</parcel-characteristics>
<origin-postal-code>${originPostal}</origin-postal-code>
<destination>
<domestic>
<postal-code>${destPostal}</postal-code>
</domestic>
</destination>
</mailing-scenario>
`;

return this.http.post<any>(this.TEST_URL, body, options)
}

查询在 Postman 上运行良好,但在 Ionic 项目(ionic serveionic run -l)上运行不佳。网上查了一下,是CORS问题Http failure response for (unknown url): 0 Unknown Error ,我在 ionic.config.json 文件中添加了以下内容

  "proxies": [
{
"path": "/price",
"proxyUrl": "https://ct.soa-gw.canadapost.ca/rs/ship/price"
}
]

API key, URL 由加拿大邮政提供,可以找到here

我遇到的错误:

Failed to load https://ct.soa-gw.canadapost.ca/rs/ship/price: Response for preflight has invalid HTTP status code 500.

14:32:24.786 shipping.ts:34 {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}

如有任何帮助,我们将不胜感激!

2018 年 6 月 15 日更新:

今天在真机上试了一下,报这样的错误: enter image description here

preflight response issue with ionic3 app on ios build only [ resolved ]Ionic UIWebView , 不幸的是,请求仍然得到同样的错误...

最佳答案

getRates(weight: number, originPostal, destPostal) {
this.http.setDataSerializer('utf8');
return this.http
.post(
this.URL,
this.body(weight, originPostal, destPostal),
this.headers(this.USER_NAME, this.PASSWORD)
);
}

private headers(username, password) {
return {
'Authorization': 'Basic ' + btoa(username + ':' + password),
'Accept': 'application/vnd.cpc.ship.rate-v3+xml',
'Content-Type': 'application/vnd.cpc.ship.rate-v3+xml;',
'Accept-language': 'en-CA'
};
}

private body(weight, originPostal, destPostal) {
return `<?xml version="1.0" encoding="utf-8"?>
<mailing-scenario xmlns="http://www.canadapost.ca/ws/ship/rate-v3">
<customer-number>MY_CUSTOMER_NUMBER</customer-number>
<parcel-characteristics>
<weight>${weight}</weight>
</parcel-characteristics>
<origin-postal-code>${originPostal}</origin-postal-code>
<destination>
<domestic>
<postal-code>${destPostal}</postal-code>
</domestic>
</destination>
</mailing-scenario>`;
}

有几个问题导致请求不起作用:

  1. ionic 中的 HttpClient 模块(Angular 模块)总是有 CORS,ionic 团队有相关的 FAQ。在我的例子中,我使用 firebase 作为后端,因此不经常使用 http,我决定切换到 Ionic Native Http
  2. XML 问题,标签 <?xml> 前有一个前导空格直到打印出错误信息的详细信息我才意识到这一点(错误信息本身也是XML格式,其余省略,因此一开始我认为它只有错误信息Server,一旦我将整条消息切成碎片并在控制台上打印出来,我发现它是非法的 xml 格式,然后我发现有一个前导空格): Error

将 XML 转换为 JSON(xml2js 让生活更轻松):

import * as xml2js from 'xml2js';

this.rateProvider.getRates(this.totalWeight, this.ORIGINAL_POSTAL_CODE, this.selectedAddress.postalCode.toUpperCase())
.then(resp => {
xml2js.parseString(resp.data, (err, jsonResp) => {
this.quotes = this.extractQuotes(jsonResp);
this.presentActionSheet(this.quotes);
this.isSelected = false;
});
})
.catch(error => {
console.log(error);
});

最后,让它工作: enter image description here

关于angular - Canada Post Rates Error in Ionic Project Response for preflight has invalid HTTP status code 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50749853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com