如何在Angular2中将Headers添加到http.get请求?

时间:2017-02-21 04:49:09

标签: angular2-services

   import {Http, Response, Headers}          from '@angular/http'; 

getHeroes (): Observable<Heros[]> {
        return this.http.get(this.heroesUrl,  {withCredentials: true}
        )
          .map(this.extractData)
          .catch(this.handleError);
      }

不要到达标题的位置和方式。

var myHeaders = new Headers();
myHeaders.append('Access-Control-Allow-Origin', '*')

它们是如何组合的?

2 个答案:

答案 0 :(得分:2)

这是您需要向http请求添加标头

的方法
$qry= "INSERT INTO students (type,class, username,password,description,reg_date) values ('$type', '$class','$username','$password','$description','$date')"; 

答案 1 :(得分:0)

//可以发送单个和多个标头

import { Http, Headers, RequestOptions } from '@angular/http';

const Url = 'http://localhost:3000/';
const headers = new Headers;
const body = JSON.stringify(
{
title: "data" 
});
headers.append('Content-Type', 'application/json');
headers.append('Access-Control-Allow-Origin', '*');
this.http.post(Url, body, { headers: headers })
.pipe(map(res => res))
.catch(err => err);

// HttpHeaders in angular 5

import { HttpHeaders } from '@angular/common/http';

let header = new HttpHeaders();
header.set('Content-Type', 'application/json');