java - post 方法不会在 Angular 中成功
问题描述
我正在为此编写一个带有 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
解决方案
JSON
您正在从您的 java 服务返回一个文本而不是一个对象。responseType
您可以通过在调用中指定来解决此问题post
:
this.httpClient.post(endpoint, formData, { responseType: 'text' })
您还可以考虑从您的 JAVA 端点返回 JSON,但这取决于您
推荐阅读
- login - 铁网刮刀 - 登录
- c# - 在 WAV 文件上接收“NAudio.MmException: NoDriver calling acmFormatSuggest”异常
- java - 我有一个对列表如何检查列表是否包含特定元素。list.contains 函数总是给出错误
- logging - 如何在相扑逻辑中找出 http 请求的响应时间
- javascript - 使用包含矢量坐标的另一个对象的对应键更改 2D 数组键名称
- python - 浏览所有文件夹和子文件夹以删除所有包含特定字符串的文件
- ios - 在我错过了颤振应用程序后,有什么办法可以回到我的旧 Xcode
- sql - 我可以用来自两个不同表的外键为两个不同的值设置一列吗?
- mysql - MYSQL 条件总是评估为 Else
- django - django.db.utils.ProgrammingError:关系“choices_keyword”的列“image”已经存在