Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.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 5-简单API调用不适用于HttpClient_Angular_Api_Http - Fatal编程技术网

Angular 5-简单API调用不适用于HttpClient

Angular 5-简单API调用不适用于HttpClient,angular,api,http,Angular,Api,Http,我尝试使用HttpClient模块调用教程中的简单API,但在控制台中得到“HttpErrorResponse:Unknown error” 这里是我打电话的地方: this.http.get('https://api.github.com/users/seeschweiler').subscribe(data => { console.log(data); }); } 对我来说很好,看 类型脚本组件 import { Component, OnInit } fr

我尝试使用HttpClient模块调用教程中的简单API,但在控制台中得到“HttpErrorResponse:Unknown error”

这里是我打电话的地方:

this.http.get('https://api.github.com/users/seeschweiler').subscribe(data => {
      console.log(data);
    });
  }
对我来说很好,看

类型脚本组件

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'my-app',
  template: `<div *ngIf="githubData">Id = {{githubData.id}}</div>`,
})
export class AppComponent implements OnInit  {
  githubData: any;

  constructor(private http: HttpClient){}

    ngOnInit(){
     this.http.get('https://api.github.com/users/seeschweiler').subscribe(data => {
        this.githubData = data;
      });
    }
}
从'@angular/core'导入{Component,OnInit};
从'@angular/common/http'导入{HttpClient};
@组成部分({
选择器:“我的应用程序”,
模板:`Id={{githubData.Id}}`,
})
导出类AppComponent实现OnInit{
数据:任何;
构造函数(私有http:HttpClient){}
恩戈尼尼特(){
this.http.get('https://api.github.com/users/seeschweiler)。订阅(数据=>{
this.githubData=数据;
});
}
}

查看错误,然后从那里进行调试

this.http.get('https://api.github.com/users/seeschweiler').subscribe(data => {
          console.log(data);
        },

    error => {
        console.log('Log the error here: ', error);
    });

      }

以下代码适用于我:- 1) At component.ts

GetData() {
    this.myService.GetUsers(this.someId).subscribe(
        success => {

        },
        errors => {

        }
    );
2) At service.ts

 GetUsers(someId: number) {
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let params: URLSearchParams = new URLSearchParams();
    params.set('someId', someId.toString());


    let options = new RequestOptions({ headers: headers, search: params });
    return this.http.get('https://api.xyz.com/users/GetUsersList', options)
        .map(res => res.json());
}
}

3) 别忘了从rxjs/operator导入“.map”

您是否在控制台中记录了其他错误?可能与CORS有关?StackBlitz链接不起作用,只需将stackoverflow更改为StackBlitz:@user184994-我认为该链接的复制/粘贴错误已经修复。@monogate-谢谢,这是一个复制粘贴错误,我想,该链接现在应该可以工作了。我一直收到“Http失败响应(未知url)”:0未知错误“