如何将 header 添加到 angular 2 中的 post 请求?
How to add header to a post request in angular 2?
//In services.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class DataService {
constructor(private http: Http) { }
fetch(){
return this.http.get('json-object-link').map(
(res) => res.json()
)
}
}
//In component
ngOnInit() {
this.dataService.fetch()
.subscribe(
(data) => this.ninjas = data
);
}
I want to add the following header to this request:
"headers":
{
"content-type": "application/json",
"access_token": "abcd"
}
试试这个
let headers = new Headers({ "content-type": "application/json", });
headers.append('access_token', "abcd");
let options = new RequestOptions({ headers: headers });
this.http
.post(url, data, options)
@Injectable()
export class UserService {
constructor (private http:HttpClient) {}
getUserByName(username: string) {
let url="http://localhost:8088/rest/user/userName";
let header=new Headers({'Content-Type': 'application/json', 'Authorization':
'Bearer '+localStorage.getItem("access_token")});
return this.http.post(url, username, {headers: header});
}
}
你可以
let headers = new HttpHeaders(); headers = headers.set(...); headers = headers.set(...); headers = headers.set(...);
或
let headers = new HttpHeaders().set(...)
.set(...)
.set(...);
参考:
如果在 angular 2
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('authentication', `${student.token}`);
let options = new RequestOptions({ headers: headers });
return this.http
.put(url, JSON.stringify(student), options)
//In services.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class DataService {
constructor(private http: Http) { }
fetch(){
return this.http.get('json-object-link').map(
(res) => res.json()
)
}
}
//In component
ngOnInit() {
this.dataService.fetch()
.subscribe(
(data) => this.ninjas = data
);
}
I want to add the following header to this request:
"headers":
{
"content-type": "application/json",
"access_token": "abcd"
}
试试这个
let headers = new Headers({ "content-type": "application/json", });
headers.append('access_token', "abcd");
let options = new RequestOptions({ headers: headers });
this.http
.post(url, data, options)
@Injectable()
export class UserService {
constructor (private http:HttpClient) {}
getUserByName(username: string) {
let url="http://localhost:8088/rest/user/userName";
let header=new Headers({'Content-Type': 'application/json', 'Authorization':
'Bearer '+localStorage.getItem("access_token")});
return this.http.post(url, username, {headers: header});
}
}
你可以
let headers = new HttpHeaders(); headers = headers.set(...); headers = headers.set(...); headers = headers.set(...);
或
let headers = new HttpHeaders().set(...)
.set(...)
.set(...);
参考:
如果在 angular 2
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('authentication', `${student.token}`);
let options = new RequestOptions({ headers: headers });
return this.http
.put(url, JSON.stringify(student), options)