user.service.ts角2 HTTP Web发布的API 2总是收到空
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 };
}
当我调用方法插入,数据总是空到达或零, 我不得不改变{ 'Accept' : 'application/json' }
线{ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept' : 'application/json' }
,因为它给了我405错误
在此先感谢大家
我认为这个问题可能有助于https://stackoverflow.com/a/45782818/2708210不太确定,但可能作为内容类型似乎是我的问题 –