post method is not going to success part in angular

huangapple go评论77阅读模式
英文:

post method is not going to success part in angular

问题

我正在编写一个使用Java的Angular项目。为此,我编写了一个Java REST服务,并从我的Angular客户端使用POST方法调用它。它能够调用REST服务,但不返回到客户端。

我在成功部分放置了一个警告。它没有打印,但在Chrome网络选项卡中显示状态码200。

Angular: -

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
英文:

I am writing an angular with java project for this I have wrote a java rest service and I am calling it from my angular client with post method. it is able to call the rest service but it is not coming to client side.

I kept an alert in success. It is not printing but in chrome network tab the status is showing 200.

Angular: -

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

Java service: -

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

I want to do some functionality in success, but the call is not coming to success part.

I got an error in the console.

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

答案1

得分: 1

你可以通过在你的post调用中指定responseType来返回文本而不是JSON对象,如下所示:

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

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

英文:

You are returning a text and not a JSON object from your java service. You can fix this by specifying the responseType in your post call:

this.httpClient.post(endpoint, formData, { responseType: &#39;text&#39; })

You can also think about returning a JSON from your JAVA endpoint, but that's up to you

huangapple
  • 本文由 发表于 2020年8月5日 18:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63262853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定