Ionic Native HTTP在Ios Simulator上返回-1状态错误



对于IOS模拟器中的单个API调用,Ionic Native HTTP返回{status:-1}

我正在使用Ionic Native HTTP来解决IOS中的CORS问题,并且有一个Wordpress API调用/wp-json/wp/v2/总是返回-1,尽管其他/wp-json/wp/v2/API调用工作非常好。

此外,这在安卓系统中运行良好,只是IOS出现了问题。

HTTP代码其中HTTP=@eionic native/HTTP/ngx

const url = https://example.com/wp-json/wp/v2/data?order=asc&order_by=id&per_page=100&after=2020-07-30 00:00:00&before=2020-07-30 23:59:59;
this.http.get(url, {}, {})
.then((data) => {
console.log('returning data');
console.log(data);
})
.catch((err) => {
console.log('erroring');
console.log(err);
console.log(err.status);
console.log(err.error); // error message as string
console.log(err.headers);
});

这个代码没有什么特别之处,它只是一个Wordpress API调用,但我总是返回-1。这个问题表明这是一个CORS问题,飞行前的OPTIONS请求被拒绝,但Native HTTP的全部目的是避免CORS,因为我无法修改服务器。

是否有其他人遇到并修复了此状态-1问题?

编辑请参阅下面的答案。

问题是URL中的空格,似乎Android和浏览器会自动将空格替换为%20,但IOS没有。

因此,新的URL应该如下所示,现在可以正常工作了。

const url = https://example.com/wp-json/wp/v2/data?order=asc&order_by=id&per_page=100&after=2020-07-30%2000:00:00&before=2020-07-30%2023:59:59;

最新更新