Angular 2:异常:响应状态为:0 对于 URL:null [英] Angular 2: EXCEPTION: Response with status: 0 for URL: null

查看:26
本文介绍了Angular 2:异常:响应状态为:0 对于 URL:null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 http.get 从我的 Angular2/ionic2 应用程序发送 HTTP GET 请求.HTTP GET 请求包含一个有效的 Linkedin 访问令牌,并且应该返回一些配置文件数据.但是调用getProfileData()时会出现如下错误:

I am trying to send a HTTP GET request from my Angular2/ionic2 app using http.get. The HTTP GET request contains a valid Linkedin access token and is supposed to return some profiledata. However, the following error occurs when getProfileData() is called:

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"

入职加载.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 之外的另一台服务器上.

It's probably a CORS issue.
Your Angular2 APP is hosted on another server than the API you're calling.

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

If you want to bypass this (only for test purposes), install this Chrome extension and add

访问控制允许来源:

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

to your headers.
For real usage you should call the API from your server(Node, PHP, Python ...) (where the angular2 APP is hosted) and pass data to your angular2 APP.

这篇关于Angular 2:异常:响应状态为:0 对于 URL:null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆