角HttpClient不发送标头这是我的代码: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.');
}
}
);}在这里,网络调试: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数据存储在‘RequestPayload’中,但在我的服务器中没有收到POST值:print_r($_POST);
Array
(
)我相信这个错误来自于在帖子中没有设置的标题,我所做的有什么不对呢?
3 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
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'});
更新:添加多个标头
let headers = new HttpHeaders();headers = headers.set('h1', 'v1').set('h2','v2');
const headers = new HttpHeaders({'h1':'v1','h2':'v2'});
UPDATE:接受HttpClient标头和params的对象映射
HttpHeaders
http.get('someurl',{ headers: {'header1':'value1','header2':'value2'} });
慕斯709654
TA贡献1840条经验 获得超5个赞
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');
添加回答
举报
0/150
提交
取消