![](/img/trans.png)
[英]Unable to set content-type = application/json angular4 post request
[英]Angular 2 - POST does not set Content-Type to application/json (its text/plain)
我创建了一个 Contactformular,我的模拟服务器正在工作,但是在 Angular 2 中,有我的创建功能:
createPatient(Name: string, Nachname: string, Geburtsdatum: string, Strasse: string, Hausnummer: string, Postleitzahl: string, Wohnort: string): Observable<boolean> {
let patient = {
Id: 0,
Name: Name,
Nachname: Nachname,
Geburtsdatum: Geburtsdatum,
Strasse: Strasse,
Hausnummer: Hausnummer,
Postleitzahl: Postleitzahl,
Wohnort: Wohnort
};
let body = JSON.stringify(patient);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.patientenUrl + "CreatePatient", body, options)
.map((r: Response) => {
console.log(r);
return <boolean>r.json();
});
身体不涉及网址,所以我的“服务器”不工作。 我在哪里失败?
我总是得到:
OPTIONS http://localhost:9177/api/v1/CreatePatient XMLHttpRequest cannot load http://localhost:9177/api/v1/CreatePatient. Response for preflight has invalid HTTP status code 415 EXCEPTION: Response with status: 0 for URL: null Uncaught Response with status: 0 for URL: null
经过一些帮助,我得到了这个解决方案:
createPatient(Name: string, Nachname: string, Geburtsdatum: string, Strasse: string, Hausnummer: string, Postleitzahl: string, Wohnort: string): Observable { let patient = { Id: 0, Name: Name, Nachname: Nachname, Geburtsdatum: Geburtsdatum, Strasse: Strasse, Hausnummer: Hausnummer, Postleitzahl: Postleitzahl, Wohnort: Wohnort }; let body = JSON.stringify(patient); let headers = new Headers({ 'content-type': 'application/json', 'accept': 'application/json'}); let options = new RequestOptions({ headers: headers }); return this.http.post(this.patientenUrl + "CreatePatient", body, headers) .map((r: Response) => { console.log(r); return r.json(); });
现在我得到了我的身体,但内容类型是“文本/纯文本”,所以现在我总是得到:
POST http://localhost:9117/api/v1/CreatePatient 415 (Unsupported Media Type) EXCEPTION: Response with status: 415 Unsupported Media Type for URL: http://localhost:9117/api/v1/CreatePatient Uncaught Response with status: 415 Unsupported Media Type for URL: http://localhost:9117/api/v1/CreatePatient
我的创建控制器功能:
[Route("[action]")] [HttpPost] public JsonResult CreatePatient([FromBody]Patient patient) { this.ResStatus.Status = false; var pat = patient; var ida = GetId(); //Prüfung ob Customer Leer oder Null pat.Id = (int)ida; patientRepository.Insert(pat); this.ResStatus.Status = true; return Json(this.ResStatus.Status); }
我解决了问题:我将 wwwroot 从 ASP.NET Core 更改为 Angular 2 项目,并包含 index.html 中的链接,我可以毫无问题地刷新它。
你为什么要 JSON.stringify 你的“身体”? 您不必这样做,它应该可以在没有 JSON.stringify() 的情况下工作
——
我的意思是,您应该只发送“患者”或“身体 = 患者”; 然后发送正文。
addBookWithObservable(book:Book): Observable<Book> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.url, book, options)
.map(this.extractData)
.catch(this.handleErrorObservable);
https://www.concretepage.com/angular-2/angular-2-http-post-example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.