Angular http请求向RESTAPI发送空数据
我正在尝试将数据从角度形式发送到端点。 这种角度的形式是在爱奥尼亚4应用程序。但我得到的只是后端的空数据。但是,当我尝试与postman相同的方法时,效果很好。我还尝试将内容类型更改为“application/json”,但它也给出了相同的结果。对于后端,我使用了一个php apiAngular http请求向RESTAPI发送空数据,angular,typescript,ionic4,Angular,Typescript,Ionic4,我正在尝试将数据从角度形式发送到端点。 这种角度的形式是在爱奥尼亚4应用程序。但我得到的只是后端的空数据。但是,当我尝试与postman相同的方法时,效果很好。我还尝试将内容类型更改为“application/json”,但它也给出了相同的结果。对于后端,我使用了一个php api // Http Options,where I set http headers httpOptions = { headers: new HttpHeaders({ 'Content-Typ
// 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.
//Http选项,其中设置了Http头
httpOptions={
标题:新的HttpHeaders({
“内容类型”:“多部分/表单数据;边界=----WebKitFormBoundary7MA4YWxkTrZu0gW”
};
//创建新用户
createUser(用户:用户):可观察{
返回此文件。http
.post(this.url+'test/create',user,this.httpOptions)
.烟斗(
重试(0),
//上面已经实现了handleerror方法
catchError(this.handleError)
);
}
}
我在chrome控制台中没有收到任何错误。
您需要将数据作为表单数据传递,如下所示:
const newUser = new FormData();
newUser.append('firstName', user.firstName);
然后在您的帖子中传递newUser。在您返回之前,写一个console.log(user);并让我知道您需要什么see@devpato这是我在控制台“User{firstName:”Hasitha“}”中得到的输出,请尝试将值作为表单数据传递。
const newUser=new FormData();newUser.append('firstName',User.firstName);然后在您的帖子中传递newUser。
为什么需要管道?您不能从后端发送错误,例如状态500吗?@devpato谢谢。我在接收输入时遇到了一个问题,没有传递值,因为表单数据是问题所在。尝试了您的方法并成功了。再次感谢。