Just diving into the world of Angular 2 and CORS
Trying to send form data from Angular 2 to a Rest controller, but encountering an error:
Response with Status:0 and Url: Null
component.ts
---------------------------------------
onSubmit(form:NgForm) {
console.log(form.value);
var job = JSON.stringify(form.value);
alert(job); //value displayed
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); //value displayed
return this.http.post('http://localhost:8080/addjobcleanup', job,options)
.map(res => res.json());
}
Rest Controller is located in a separate repository
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";
}