My Angular application includes a form and a submit button. The issue arises when I fill out the form and submit it; if the database injection operation is successful, I want to display a success message and clear the form automatically. I attempted to achieve this by utilizing the subscribe
method, but for some reason, the code never enters this method and the desired functionalities do not get triggered.
insertRecord(form : NgForm){
this.service.postAsset(form.value).subscribe(res =>{
this.toastr.success('Inserted successfully', 'Asset Register');
this.resetForm(form);
});
}
The postAsset
method works correctly, adding the value to the database table. However, the subscribe
method does not seem to be functioning as expected. What could be causing this problem? Any assistance would be greatly appreciated.
Edit: Upon request, here is the implementation of the postAsset()
method;
postAsset(formData: Asset){
return this.http.post(this.rootURL + '/Create', formData);
}
Edit 2: The error occurs a few seconds after submitting the form;
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:13804:51)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2781:31)
at Object.onInvokeTask (http://localhost:4200/vendor.js:59285:33)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:2780:60)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:4200/polyfills.js:2553:47)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:4200/polyfills.js:2856:34)
at invokeTask (http://localhost:4200/polyfills.js:4102:14)
at XMLHttpRequest.globalZoneAwareCallback (http://localhost:4200/polyfills.js:4139:21)