簡體   English   中英

Angular 2 - POST 未將 Content-Type 設置為 application/json(其文本/純文本)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM