user.service.ts
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import * as myGlobal from '../global';
@Injectable()
export class UserService {
constructor(private http: Http) {}
postUser(user:any[]) {
let headers = new Headers({ 'Content-Type': 'application/x-www-form-
urlencoded; charset=UTF-8', 'Accept' : 'application/json' });
return this.http.post(myGlobal.myUrl + 'user/insert', JSON.stringify(user)
, {headers: headers});
}
}
user.component.ts
submitForm(data: any):void{
console.log(data);
this._service.postUser(data)
.subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
Web API
[HttpPost]
[Route("api/user/insert")]
public HttpResponseMessage insertVehiclesDevice(modelUser data)
{
//stuff
return new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
}
When I invoke method Insert, the data always arrive in null or zero,
I had to change the { 'Accept' : 'application/json' }
line for { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept' : 'application/json' }
because it gave me 405 error
Thanks in advance to everyone