Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular 6中添加http头_Angular_Http_Visual Studio Code_Http Headers - Fatal编程技术网

在Angular 6中添加http头

在Angular 6中添加http头,angular,http,visual-studio-code,http-headers,Angular,Http,Visual Studio Code,Http Headers,有人能告诉我这是否是在Angular 6中向http请求添加头的正确方法吗 当我通过SwaggerUI进行呼叫时,我可以看到标题应该是: url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff' 因此,我添加了以下内容: let headers: HttpHeaders = new HttpHeaders(); headers =

有人能告诉我这是否是在Angular 6中向http请求添加头的正确方法吗

当我通过SwaggerUI进行呼叫时,我可以看到标题应该是:

url -X GET --header 'Accept: application/json' --header 'zumo-api-version: 2.0.0' 'https://myurl/tables/stuff'
因此,我添加了以下内容:

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('HttpHeader1', 'Accept:application/json');
headers = headers.append('HttpHeader2', 'zumo-api-version:2.0.0');
然后是电话:

getStuff(){
    return this.http.get('https://myurl/tables/stuff', {headers})
  }
没有失败,但没有回报,我知道应该有

谢谢

更新

我刚刚注意到我通话中的url实际上是https而不是http,这有什么区别吗

getStuff(){
        return this.https.get('https://myurl/tables/stuff', {headers})
      }

设置标题的正确格式如下所示

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('zumo-api-version', '2.0.0');
url-xget--header'Accept:application/json'--header'zumoapi版本:2.0.0''https://myurl/tables/stuff“

在上面的请求中,头键的名称是接受zumo api版本前面的文本:

标题基本上设置为键/值对

设置的正确方法是

let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
headers = headers.append('zumo-api-version', '2.0.0');
角度6格式:

let headers = new HttpHeaders({
    'Accept': 'application/json',
    'zumo-api-version': '2.0.0'
});

尝试下面的代码可能会对您有所帮助

let headers = new HttpHeaders().set('Accept': 'application/json').set('zumo-api-version': '2.0.0')

你没有得到任何回报,因为你没有订阅该活动。将
.subcribe
添加到该函数中,无论您在哪里调用它eg

getStuff().subscribe(数据=>{
控制台日志(数据);
}
)

因此,您订阅的
数据
包含所有响应以及您需要了解的有关该调用的所有信息


您可以从这里阅读更多内容

我已经在代码中这样做了

httpOptions={ headers: new HttpHeaders({ 'Content-Type': 'application/json'})};
 this.httpOptions.headers = this.httpOptions.headers.append('Token', this.Token);
然后在我的http.get调用中,我完成了以下操作:

return this.http.get<JSON>(this.API_ADDRESS+'/api/RemoveEmployee/'+id,this.httpOptions
返回this.http.get(this.API_地址+'/API/RemoveEmployee/'+id,this.httpOptions
角度6+

申报区:

初始化:

用法:

返回这个.u http.get(`${url}',{headers:this.httpOptionsNoAuth.headers});

您得到的
HttpHeader1
实际上应该是标题名,即
Accept
,而您得到的
Accept:application/json
应该是值,即
application/json
,所以您真正想要的是
headers.append('Accept',application/json'))
确保您在HTTP调用中也调用了
subscribe
。可观察对象是惰性的,因此它只会在某个对象已订阅后进行HTTP调用。这意味着使用角度拦截器设置HTTP头let options=new RequestOptions({headers:headers});返回此值。_HTTP.get(this._url,options)请确保您使用上述语句调用get request,方法是在request callsame thing中传递头-无数据,是的,我会大摇大摆地返回它。如果我错过了订阅,我会在早上回家时间测试它。如果是get请求,则不会有内容,因此内容类型更改“Accep”并不重要t':'application/json'到'Content Type':'application/json'设置头的简短方法:)谢谢,那么调用仍然应该是:getStuff(){返回this.http.get('',{headers})}?先生,我试过你的代码,但我得到了错误类型错误:CreateListFromArrayLike在控制台的非对象上被调用。你能告诉我如何修复吗?在试图帮助他人之前,至少要学会格式化你自己的代码。@Jhorladestrella谢谢你的建议。下一次我会处理。向下投票,因为添加标题的语法不准确。正确的方法是
append
,而不是
set
。[
httpOptionsNoAuth : any;
constructor(){
    this.httpOptionsNoAuth = {
        headers: new HttpHeaders().set('No-Auth', 'true')
    };
}
return this._http.get<any>(`${url}`, { headers: this.httpOptionsNoAuth.headers});