郎朗坤
2019-07-09 13:31:21
角HttpClient不發(fā)送標(biāo)頭這是我的代碼:import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';logIn(username: string, password: string) {
const url = 'http://server.com/index.php';
const body = JSON.stringify({username: username,
password: password});
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json; charset=utf-8');
this.http.post(url, body, {headers: headers}).subscribe(
(data) => {
console.log(data);
},
(err: HttpErrorResponse) => {
if (err.error instanceof Error) {
console.log('Client-side error occured.');
} else {
console.log('Server-side error occured.');
}
}
);}在這里,網(wǎng)絡(luò)調(diào)試:Request Method:POST
Status Code:200 OK
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:46
Content-Type:text/plain數(shù)據(jù)存儲(chǔ)在‘RequestPayload’中,但在我的服務(wù)器中沒有收到POST值:print_r($_POST);
Array
(
)我相信這個(gè)錯(cuò)誤來自于在帖子中沒有設(shè)置的標(biāo)題,我所做的有什么不對(duì)呢?
3 回答

蕪湖不蕪
TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超7個(gè)贊
HttpHeader
let headers = new HttpHeaders();headers = headers.set('Content-Type', 'application/json; charset=utf-8');
const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
更新:添加多個(gè)標(biāo)頭
let headers = new HttpHeaders();headers = headers.set('h1', 'v1').set('h2','v2');
const headers = new HttpHeaders({'h1':'v1','h2':'v2'});
UPDATE:接受HttpClient標(biāo)頭和params的對(duì)象映射
HttpHeaders
http.get('someurl',{ headers: {'header1':'value1','header2':'value2'} });

慕斯709654
TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
let headers = new HttpHeaders(); headers = headers.append('key', 'value');
let headers = new HttpHeaders().append('key', 'value');
let headers = new HttpHeaders(); let headers1 = headers.append('key', 'value');
添加回答
舉報(bào)
0/150
提交
取消