我在Ionic 4项目中使用@离子型本机/http登录用户,通过POST方法发送带有UserId和密码的主体以及带有“Content”:'application/json‘的标题。它在android上运行良好,但在iOS上,它的响应是一个http 400错误。
Dependencies:
"@ionic-native/http": "^5.3.0",
"cordova-plugin-advanced-http": "^2.0.9",我厌倦了使用@转角/http,但它在浏览器、安卓和iOS上出现了一个CORS错误。我不能更改服务器端代码来启用CORS。
代码:
import { HTTP } from '@ionic-native/http/ngx';
// Login method
async login(userId, password) {
// login user
const httpBody = {
'UserId': userId,
'Password': btoa(password)
};
const httpHeader = {
'Content-Type': 'application/json',
};
const user = await this.http.post(this.loginURL, httpBody, httpHeader);
return JSON.parse(user.data);
}使用StatusCode 200的预期结果响应
StatusCode 400的实际结果响应
发布于 2019-10-08 21:16:27
我也有同样的问题;在我的例子中,我通过将数据序列化程序设置为'utf8'和将Content标头设置为utf8来修复它。
this.http.setDataSerializer('utf8');
const httpHeader = {
'Content-Type': 'application/application/x-www-form-urlencoded'
};https://stackoverflow.com/questions/55917307
复制相似问题