我是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'? –