Angular 2 - POST不将Content-Type设置为application / json(其text / plain)

时间:2016-10-11 14:39:36

标签: angular http-post

我创建了一个Contactformular,我的模拟服务器正在运行,但在Angular 2中,有我的Create-function:

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();
        });

现在我得到了我的身体,但Content-Type是'text / plain' 所以现在我总是得到:

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链接。

2 个答案:

答案 0 :(得分:1)

为什么JSON.stringify你的'身体'?您不必这样做,它应该在没有JSON.stringify()

的情况下工作

-

我的意思是你应该发送'耐心'或做一个“身体=病人”;然后发送正文。

答案 1 :(得分:0)

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