【问题标题】:angular spring boot request with image error带有图像错误的角度弹簧启动请求
【发布时间】:2020-11-01 16:29:03
【问题描述】:

所以我试图以 x-wwww-form-urlencoded 的方式从 Angular 向我的 Spring Boot 后端发送图像以及一些文本。 这是角度服务方法:

img(tags:HTMLInputElement, des:HTMLInputElement, selectFile){
    let url = "http://localhost:8080/api/v1/add_item"

    const body = new HttpParams()
    body.set("img", selectFile)
    body.set("tags", tags.value)
    body.set("des", des.value)

    return this.http.post<Isecurity[]>(url, body.toString(),{
      headers:new HttpHeaders()
      .set('Content-Type', 'application/x-www-form-urlencoded')
    } )
  }

在其他方面:

@PostMapping("add_item")
    @CrossOrigin
    public  Map<String, Boolean> add_item(@RequestParam MultipartFile img, @RequestParam String tags, @RequestParam String des){
        Map<String, Boolean> values = new HashMap<>();
        values.put("response", true);
        return values;}

我认为是,因为@RequestParam 标签,因为我得到的唯一响应是 500 server error...

【问题讨论】:

  • 您能否提供有关 500 错误的更多详细信息? (提供导致500 http错误状态的异常

标签: angular spring spring-boot web x-www-form-urlencoded


【解决方案1】:

我认为您以错误的方式传递参数。您只是将 params 的名称保留为 body,但它实际上不是 body。

您需要在 url 之后使用 '?' 传递参数。

因此,您必须将 .ts 文件中的代码更新为以下几行:

img(tags:HTMLInputElement, des:HTMLInputElement, selectFile){
let url = "http://localhost:8080/api/v1/add_item?"

const params= new HttpParams()
params.set("img", selectFile)
params.set("tags", tags.value)
params.set("des", des.value)

return this.http.post<Isecurity[]>(url+params.toString(),{
  headers:new HttpHeaders()
  .set('Content-Type', 'application/x-www-form-urlencoded')
} )

}

【讨论】:

    猜你喜欢
    • 2019-02-03
    • 2019-07-14
    • 2018-07-27
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2015-01-22
    • 2020-10-06
    相关资源
    最近更新 更多