我正在尝试使用http.get从我的Angular2 / ionic2应用程序发送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
仍然:
>在www.hurl.it上测试时,具有相同URL的GET请求有效
>从应用程序调用的GET请求使用不同的URL,例如让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(); } ... }
这可能是一个CORS问题.
您的Angular2 APP托管在您正在呼叫的API之外的其他服务器上.
您的Angular2 APP托管在您正在呼叫的API之外的其他服务器上.
如果要绕过此(仅用于测试目的),请安装this Chrome extension并添加
Access-Control-Allow-Origin:
到你的标题.对于实际使用,您应该从您的服务器(Node,PHP,Python …)(托管angular2 APP)调用API,并将数据传递给angular2 APP.