angular - 为什么我的 POST 数据没有在 Ionic 4 中正确提交?
问题描述
我正在将我的 Ionic 应用程序升级到版本 4,并且遇到了一个奇怪的问题,我不确定数据是否正确提交到我的 API。
我已将其精简到尽可能基本的程度,API 仍然返回 401 Unauthorized 错误。
我知道 API 有效,因为当我通过 Postman 提交相同的内容时,我会收到预期的数据......
我正在导入HttpClient
...
import { HttpClient, HttpHeaders } from '@angular/common/http';
这是我的精简功能,它在单击提交按钮时运行。
onSubmit() {
const headers = new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded'
});
let body = {
'username': 'test',
'password': 'test',
}
this.http
.post(this.apiService.url, body, { headers: headers })
.subscribe((data: any) => {
console.log(data);
}, error => {
console.log('error');
});
}
这一切都在 Ionic 3 中完美运行,所以我真的很困惑为什么它现在不起作用。
解决方案
这篇文章为我解决了这个问题。显然 Angular 5+ 会为您处理标题,所以我所要做的就是删除它们并且它起作用了。
onSubmit() {
let body = {
'username': 'test',
'password': 'test',
}
this.http
.post(this.apiService.url, body)
.subscribe((data: any) => {
console.log(data);
}, error => {
console.log('error');
});
}
推荐阅读
- wpf - WPF 网格意外调整大小
- javascript - return 和 console.log() 有什么区别
- java - 尝试发布 WebLogic 12.2.1 应用程序时解析 application.xml 时出错
- python - 如何在 Dataframe 中按 Timedelta 划分 float64 数字?
- java - 尽管使用了合乎逻辑的方式,但无法绘制圆圈
- delphi - 为什么使用我用来通过传真发送报告的代码,传真的第二页被截断了一半?
- loops - 显示范围内给定数字的倍数
- r - 在闪亮的应用程序中基于用户输入(使用预定义的基于分类的调色板)为传单多边形着色
- ssis - 对于每个循环容器 SSIS
- c++ - 标准::设置
不会编译