【问题标题】:post method is not going to success part in angularpost 方法不会在 Angular 中成功
【发布时间】:2020-08-05 10:06:02
【问题描述】:

我正在为此编写一个带有 java 的 angular 项目我已经编写了一个 java rest 服务,我正在使用 post 方法从我的 angular 客户端调用它。它能够调用其余的服务,但它没有来到客户端。

我成功地保持了警报。它没有打印,但在 chrome 网络选项卡中,状态显示为 200。

角度:-

 uploadFileToActivity() {
   const endpoint = 'http://localhost:8090/sendmails';
   let config =  {headers: new  HttpHeaders({})};
   const formData: FormData = new FormData();
   formData.append('fileupload', this.fileToUpload);
      
   this.httpClient.post(endpoint, formData).subscribe(data => {
   alert();
   })
  }

Java 服务:-

    @PostMapping(value = "/sendmails", headers = "content-type=multipart/*")
    public ResponseEntity<String> sendEmails(@RequestParam("fileupload") MultipartFile ExcelDataFile) {
        return new ResponseEntity<String>("Success", HttpStatus.OK);
    }

我想成功地做一些功能,但调用没有成功。

我在控制台中遇到错误。

error: {error: SyntaxError: Unexpected token S in JSON at position 0 at JSON.parse (<anonymous>) at XMLHtt…, text: "Success"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:8090/sendmails"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:8090/sendmails"
__proto__: HttpResponseBase

【问题讨论】:

  • 你有什么错误吗?
  • @PatrikDendis 我没有收到任何错误
  • 您可以尝试在 Java 服务中创建简单的 GET 并从后端获取此信息吗?
  • 我可以使用get方法从后端获取数据
  • 那么您可以签入例如失败的 Chrome XHR 请求吗?

标签: java angular spring-boot


【解决方案1】:

您正在从您的 java 服务返回一个文本而不是 JSON 对象。您可以通过在 post 调用中指定 responseType 来解决此问题:

this.httpClient.post(endpoint, formData, { responseType: 'text' })

您也可以考虑从您的 JAVA 端点返回 JSON,但这取决于您

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 2019-05-12
    • 2018-01-26
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多