Angular 4不会在http请求中包含头部信息。
Angular 4不会在http请求中包含头部信息。
我正在尝试一个非常简单的Angular 4的HTTP请求。当我检查Chrome开发者工具时,我无法看到HTTP头信息。
const headers: Headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Authorization', 'Bearer: 123213');
this.http.post('https://127.0.0.1:502', JSON.stringify(token), {headers: headers}).subscribe();
我是否遗漏了什么?
Angular 4版本之后,/http模块已被弃用,包含其中所有的类。现在应该使用angular/common/http模块。如果仍使用旧版本,可以按照以下方式进行操作:
import { HttpHeaders } from '/common/http'; const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'my-auth-token' }) }; this.http.post( "http://localhost:3000/contacts", JSON.stringify({id: 4, name: 'some'}), httpOptions ).subscribe();
对于旧版本,可以按照以下方式操作:
import {Http, Headers, RequestOptions} from '/http'; export class AppComponent { constructor(private http: Http) { const headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('authentication', `hello`); const options = new RequestOptions({headers: headers}); this.http.post( "http://localhost:3000/contacts", JSON.stringify({id: 4, name: 'some'}), options ).subscribe(); }
需要确保从/http导入的对象正确:
import {Http, Headers, RequestOptions} from '/http';
如果仍然看不到请求头,可能是因为你使用的服务器不允许请求头。当浏览器向其他源发出请求时,它会发送access-control-headers-request头来检查服务器是否允许自定义头。如果服务器未配置为允许自定义头,则在随后的请求中将无法看到它们。
根据Headers的文档,该类已被弃用,建议使用HttpHeaders类。然而,我仍无法使其工作(可能是因为我对语法不够了解),所以如果你能更新你的答案,使用非过时的类,我将不胜感激。
是的,/http模块已被弃用,包含其中所有的类。现在应该使用/common/http模块。我会添加到答案中。谢谢。
Headers已被弃用,应该使用HttpHeaders。
Angular 4 doesn't include headers to http request的问题出现的原因是因为在Angular 4中,http请求默认不包含headers。解决这个问题的方法是通过在构造函数中设置headers,并在请求方法中将该headers传递给http请求。
解决方法如下:
首先,在构造函数中创建一个私有变量http,并在构造函数中初始化headers,并设置content-type为application/json。
constructor(private http: Http) { this.headers = new Headers(); this.headers.append('content-type', 'application/json'); }
然后,在请求方法中,将url、jsonData和headers作为参数传递给http.post方法,并使用map方法将返回的响应转换为json格式。
return this.http.post(url, jsonData, this.headers).map((resp: Response) => resp.json());
通过以上的解决方法,我们可以在Angular 4中包含headers到http请求中。这样,我们就能够正确地发送带有headers的请求,以满足特定的服务器要求。
Angular 4在发送http请求时没有包含headers的原因是因为在使用HttpClient时,需要手动设置headers。解决方法是在发送请求时,使用HttpHeaders类手动设置headers。具体的解决方法如下:
首先,如果你使用HttpClient而不是Http,你的代码应该像这样:
login(credintials: Authenticate): Observable{ const body = { username: credintials.username, password: credintials.password, grant_type: 'password', client_id: credintials.client_id }; const headers = new HttpHeaders() .set('Content-Type', 'application/x-www-form-urlencoded') .set('Authorization', 'Basic loremlorem'); return this.http.post(`${baseUrl}/uaa/oauth/token`, body, { headers: headers } ); }
另外,如果你的参数是可选的,你应该像这样追加参数:
let params: HttpParams = new HttpParams(); if (param) { params = params.append( 'page', param.page ); }
源代码如下:
/** * Construct a new body with an appended value for the given parameter name. */ append(param: string, value: string): HttpParams;
通过以上的代码调整,就可以解决Angular 4在发送http请求时没有包含headers的问题。