繁体   English   中英

Post Request 返回状态码 500 Angular 请求到 Spring Boot 后端

[英]Post Request return status code 500 Angular request to spring boot backend

使用以下请求时出现 500 状态代码错误

createPost(postPayload: CreatePostPayload): Observable<any> {
    return this.http.post('http://localhost:8080/api/posts/', postPayload);
  }

请求在邮递员上工作正常。

使用 HttpClient 尝试一下。 检查你 post() 正文和标题

 createPost(postPayload: CreatePostPayload): Observable<any> {
        const headers = new HttpHeaders().set('Content-Type', 'application/json');
        return this.http.post('http://localhost:8080/api/posts', postPayload, {    
            observe: 'response',    
            headers: headers           
        });
      }

所以我终于能够解决这个问题。 事实上,身份验证令牌未包含在 post 请求的标头中,这就是它在邮递员上运行良好的原因。 感谢 Murugan 和 Ahmed,他们暗示要检查标题,我设法像这样解决了这个问题

  createPost(postPayload: CreatePostPayload): Observable<any> {

    const headers = new HttpHeaders().set("Authorization", this.localStorage.retrieve('authenticationToken'));

    return this.http.post('http://localhost:8080/api/posts/', postPayload, {    
        observe: 'response',    
        headers: headers           
    });
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM