英文:
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 = '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 service: -
@PostMapping(value = "/sendmails", headers = "content-type=multipart/*")
public ResponseEntity<String> sendEmails(@RequestParam("fileupload") MultipartFile ExcelDataFile) {
return new ResponseEntity<String>("Success", 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 (<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
答案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: 'text' })
You can also think about returning a JSON from your JAVA endpoint, but that's up to you
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论