Angular 4.3 Httpclient不发送内容类型标题

时间:2017-08-28 17:55:55

标签: angular

如何设置httpheaders,我添加此代码

  public post (servicio: string, item: any): Observable<any> 
  {
    const url = `${SERVER_URL}${servicio}`;   
    const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8')
    return this.http.post(url, item, {headers: headers})
     .do(data => console.log('All: ' + JSON.stringify(data)))
     .catch(this.handleError);
  }

在chrome或fiddler中,请求是

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:56962
Origin:http://evil.com/
Referer:http://localhost:4200/entidad
User-Agent:Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Mobile Safari/537.36

2 个答案:

答案 0 :(得分:0)

最后工作不在标题中,在asp.net核心中我添加以下内容

  app.UseCors(builder =>
              builder.WithOrigins("http://localhost:4200")
                           .AllowAnyHeader()
                           .AllowAnyMethod());
            app.UseMvc();

答案 1 :(得分:-1)

去做这件事:

var headers = new Headers();
headers.append('Content-Type', 'application/json; charset=utf-8');