[英]I can't send data using ```http.post```
因此,当我尝试向后端发送发布请求时,我得到了“响应代码:500”,并且在 API 端接收到的数据对象是空的,我尝试使用邮递员,它工作得很好。
return htpp.post(link,data,headers).subscribe(response=>{
console.log(response);
},
error=>{
console.log(error);
});```
this is my function call for the same
HTTP 状态码 500 是一个通用错误响应,当错误来自前端开发人员时,您必须检查或/并执行以下步骤:
祝你好运
我面临同样的问题。 我是 Angular 和 TypeScript 的新手。
我发现发生错误是因为执行帖子时变量的名称与 API 中的预期不同。 当一个 http POST 请求被执行时,变量名以“下划线”前缀发送,因为 JS 和 TS 中使用的命名约定。
就我而言,我的 API 需要一个名为“{description:string}”的字段,但在从我的 Angular 客户端发送的请求正文中,它发送“_description:一些描述”而不是“描述:一些描述”,然后在我的 API 中它引发 NullPointerException 因为请求正文为空。
如果我尝试手动发送具有正确字段名称且不带下划线“{description:Some description}”的请求正文,则请求成功!
let data = {description: 'some description'}
this.http.post(`${this.baseUrl}/api`, data, {headers: {'Content-Type':"application/json"}});
在我的类中,对象声明如下:
export class SomeExampleClass{
constructor(private _description: string ) {
}
get description(): string {
return this._description;
}
set description(value: string) {
this._description = value;
}
然后,当我像这样执行 http Post 请求时:
let data = new SomeExampleClass("some description")
this.http.post(`${this.baseUrl}/api`, data, {headers: {'Content-Type':"application/json"}});
console.log(data)
日志结果是:
Object {_description: "some description"}
然后发生错误 500,因为发送的请求正文与 API 中的预期不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.