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不太確定,但可能作爲內容類型似乎是我的問題 –