Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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 如何在API调用中设置头值_Angular_Ionic Framework - Fatal编程技术网

Angular 如何在API调用中设置头值

Angular 如何在API调用中设置头值,angular,ionic-framework,Angular,Ionic Framework,获取401状态,甚至为每个请求传递应用程序id和密钥 let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded',}); let options = new RequestOptions({ headers: headers }); options.headers.set('Accept', "application/json,text/xml "); options.headers.set

获取401状态,甚至为每个请求传递应用程序id和密钥

let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded',});

let options = new RequestOptions({ headers: headers });
 options.headers.set('Accept', "application/json,text/xml ");
 options.headers.set('X-AYLIEN-NewsAPI-Application-ID', "ff6**d33");
 options.headers.set('X-AYLIEN-NewsAPI-Application-Key', "b7445d942********7c06e");
API调用:

  this.http.get(url)
    .subscribe(res => {
    resolve(res.json());
    }, (err) => {
    reject(err);
    });
    });
    }
这是我用来在标题中设置值的代码,但它不起作用。

请尝试以下代码:

在职:

第一:

从'@angular/common/http'导入{HttpHeaders}


这段代码对我来说很好

你的代码在哪里?你用这个头调用API的代码在哪里?这是最适合我的代码:This.headers=new-HttpHeaders({'Content-Type':'application/json','appInstanceCode':''+This.appCode,'token':''+This.token});使用HttpHeaders classbut new RequestOptions({headers:headers})不会使用httpHeader@PrashantPimpalei是新来的。请帮帮迈格拉德,它帮了你!
private url = "your_url";
private headers: any;

this.headers = new HttpHeaders({ 'Content-Type': 'application/json', 
'your_key': '' + value, 'your_key': '' + value });

getRecords(): Promise<any>{
    const url = `${this.url}/end_point`;
    return this.http.get(url, {headers: this.headers })
    .toPromise()
    .catch(this.handleError);
  }
 this.your_service_obj.getRecords()
      .then(response => {
      // console.log(response)
      },
      err => {
      })
getDataGetAuth(url) {

    return new Promise((resolve, reject) => {
        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });

        let options = new RequestOptions({ headers: headers });
         options.headers.set('Content-Type', 'application/json' );
         options.headers.set('Authorization', 'Bearer '+ sessionStorage.getItem("token") );

        this.http.get( MainURL+url,options)
        .subscribe(res => {
            resolve(res.json());
        }, (err) => {
            reject(err);

        });
    });
}