【发布时间】:2019-07-18 06:57:48
【问题描述】:
在 Angular(使用 Ionic)中使用 http 模块进行发布请求时,我无法更改标头。 这是我的代码:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
const apiUrl = "https://webhook.site/c2d56330-84d4-47cf-9f98-472f7eac8000";
@Injectable({
providedIn: 'root'
})
export class APIService {
constructor(private http: HttpClient) { }
getToken(){
var body = {
'data1': 'data2',
'somedata3': 'data4',
};
let headers = new HttpHeaders().append('Content-Type', 'application/json');
this.http.post(apiUrl, JSON.stringify(body), {headers}).subscribe(data =>
console.log(data));
console.log(headers.get('Content-Type')); //return 'application/json'
}
}
一切正常,但它仍然发送标头“content-type: text/plain”而不是“content-type: application/json”。
是不是我打错字了?
【问题讨论】:
-
尝试在没有 JSON.stringify 的情况下传递你的身体
-
这可能有助于解释它。 stackoverflow.com/questions/45286764/…
标签: javascript angular ionic-framework