How to POST JSON and a file to web service with Angular?我尝试了此链接,但这会导致错误请求错误enter code here
enter link description here
这是我的角度2代码
runImportRecordsJob(importRecords: any){
let options = new RequestOptions();
let formData = new FormData();
let f={
'country':"india",
'bug':"bug",
};
formData.append("file", importRecords.file, importRecords.file.name);
formData.append("data",f);
const headers = new Headers();
headers.append('Content-Type', undefined);
return this.http.post(this.constantService.URL+'/data/fileupload',formData,{
headers: headers}).map(this.constantService.extractData)
.catch(this.constantService.handleError);
}
这是spring rest webservice
@ResponseBody
@RequestMapping(value = "/data/fileupload", method = RequestMethod.POST)
public String postFile(@RequestParam(value="file", required=false) MultipartFile file,
@RequestParam(value="data") Object data) throws Exception {
System.out.println("data = " + data);
return "OK!";
}
尝试调用此Web服务时,它会向我显示错误错误请求错误