My web api method is not hitting from angular 2, even no error in console but same is working fine when i am checking with Jquery ajax call.
API Method---
// POST api/values
public void Post([FromBody]Customer objCustomer)
{
System.Xml.Serialization.XmlSerializer writer =
new System.Xml.Serialization.XmlSerializer(typeof(Customer));
var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//SerializationOverview.xml";
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, objCustomer);
file.Close();
return;
}
angular 2 call--- Not working
saveCustomer(): Observable<Customer[]>{
var body = {"userName": "sonoo", "Age": "56000"}
this.headers.append('Content-Type', 'application/json');
var options = new RequestOptions({method:RequestMethod.Post, headers: this.headers});
return this.http.post("http://localhost:57899/api/values", body,options)
.map(res => res.json().subscribe(
(data) => console.log(data)));
}
Note: Get method is working fine. Thanks
Looks like the problem is in your ts code. Try this instead:
saveCustomer(): Observable<Customer[]>{
const body = { userName: 'sonoo', Age: '56000'};
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post<Customer>('http://localhost:57899/api/values', body, { headers });
}
You also need to return the Customer
object from your API like this:
return this.CreatedAtRoute("DefaultApi", new { id = customer.id}, customer);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.