我是Angular 2和CORS的新手從Angular2到Rest API的Http POST [CORS]
我想將表單數據從Angular 2發佈到Rest控制器。但是我發現了以下錯誤
具有狀態響應:0和網址:空
component.ts
---------------------------------------
onSubmit(form:NgForm) {
console.log(form.value);
var job = JSON.stringify(form.value);
alert(job); //getting the value
this.jobCleanupService.addJobCleanUp(job).subscribe(
data => alert(data),
error => alert(error)
);
}
jobcleanup.service.ts
----------------------------------
addJobCleanUp(job:any){
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
alert('service'+ job); //getting the value
return this.http.post('http://localhost:8080/addjobcleanup', job,options)
.map(res => res.json());
}
休息控制器在不同的回購
RestController
---------------------------
@CrossOrigin(origins = "http://localhost:4200/newjobcleanup")
@RequestMapping(value = "/addjobcleanup" ,method = RequestMethod.POST)
@ResponseBody
public String addJobCleanUp(@PathVariable String job) {
logger.info("Add Job Clean Up");
return "Success";
}
您使用的是正確的網址嗎?我看到localhost:4200和localhost:8080 –
您是否已將代理配置添加到您的'package.json'? –