angular - 角度离子http请求将空数据发送到rest API
问题描述
我正在尝试将数据从角度形式发送到端点。这种角度形式在 ionic 4 应用程序中。但我得到的只是后端的空数据。但是当我尝试使用邮递员时,它工作正常。我也尝试将内容类型更改为“应用程序/json”,但它也给出了相同的结果。对于后端,我使用的是 php api。
// Http Options,where I set http headers
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'
};
// Create a new user
createUser(user: User): Observable<User> {
return this.http
.post<User>(this.url + 'test/create' , user , this.httpOptions)
.pipe(
retry(0),
// handle error method is already implemented above
catchError(this.handleError)
);
}
}
I'm not receiving any errors in chrome console.
解决方案
您需要将数据作为表单数据传递,如下所示:
const newUser = new FormData();
newUser.append('firstName', user.firstName);
然后在您的帖子中传递 newUser。
推荐阅读
- qt - Missing memberfunction of QOpenGLExtraFunctions
- python - 如何找到设置和打印值的相似之处
- three.js - THREE.js:如何使用四元数进行旋转和翻译
- html - Why does flex container have such height?
- postgresql - 如何在 PostgreSQL 中创建“lo 数据类型”
- html - 我应该使用 CSS 过滤器,因为它具有工作草案状态
- asp.net-mvc - 如何在 WEB API 中生成 REST 完整服务
- firebase - 使用 vuejs 和 firestore 进行地理定位
- macos - “$ mdls -recursive”是否存在?
- java - Android Root Check fails on samsung tab while executin /system/bin/which su