Angular 2:EXCEPTION:响应状态:URL为0:null

Han*_*nes 17 linkedin ionic2 angular

我正在尝试使用我的Angular2/ionic2应用程序发送HTTP GET请求http.get.HTTP GET请求包含有效的Linkedin访问令牌,并且应该返回一些profiledata.但是,getProfileData()调用时会发生以下错误:

3     229881   group    EXCEPTION: Response with status: 0  for URL: null
4     229895   error    EXCEPTION: Response with status: 0  for URL: null
5     229909   groupEnd
6     229950   error    Uncaught Response with status: 0  for URL: null, http://192.168.178.49:8100/build/js/app.bundle.js, Line: 95774
Run Code Online (Sandbox Code Playgroud)

仍然:

  • 在www.hurl.it上测试时,具有相同URL的GET请求有效
  • 从应用程序调用的GET请求使用不同的URL,例如 let URL = "https://httpbin.org/get?name=hannes"

入职-load.ts:

import { Component } from '@angular/core';
import { NavController, Platform, Alert } from 'ionic-angular';
import { OnboardingHelloPage } from '../onboarding-hello/onboarding-hello';
import { CordovaOauth, LinkedIn } from 'ng2-cordova-oauth/core';    
import { Http, Headers, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/map'


@Component({
  templateUrl: 'build/pages/onboarding-load/onboarding-load.html',
})
export class OnboardingLoadPage {
  private data;
  private code;

constructor(private navCtrl: NavController, private platform: Platform, private http: Http) {
    this.http = http;
    this.navCtrl = navCtrl;
  }

public getProfileData() {
  let URL = "https://api.linkedin.com/v1/people/~?oauth2_access_token=SOMEVALIDTOKEN&format=json";
  //let URL = "https://httpbin.org/get?name=hannes"

  this.http.get(URL)
    .map(res => res.json())
    .subscribe(data => {
      this.data = data;
      console.log(JSON.stringify(data));
  });
  // go to next page
  this.viewOnboardingHello();
}
...
}
Run Code Online (Sandbox Code Playgroud)

Jan*_*lli 13

这可能是一个CORS问题.
您的Angular2 APP托管在您正在呼叫的API之外的其他服务器上.

如果您想绕过此选项(仅用于测试目的),请安装此Chrome扩展程序并添加

访问控制允许来源:

到你的标题.
对于实际使用,您应该从服务器(Node,PHP,Python ...)(托管angular2 APP)调用API,并将数据传递给angular2 APP.