【问题标题】:ionic 2 , error in getting data from serverionic 2,从服务器获取数据时出错
【发布时间】:2016-12-15 13:29:06
【问题描述】:

我是 ionic 2 的新手,现在我正在尝试从主机获取数据但遇到此错误。我怎样才能克服这个错误。谢谢你

我遇到了这个错误

XMLHttpRequest cannot load http://example.com/jsondata . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

这是我的打字稿文件

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Http , Headers} from '@angular/http';
import 'rxjs/add/operator/map'

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
})

export class HomePage {

  url:string;
  data:any[];

  constructor(public navCtrl: NavController , public http : Http) {

    this.getdata();

  }

  getdata(){

    this.url="http://example.com/jsondata";

    this.http.get(this.url).map(res => res.json()).subscribe( res =>{
      console.log(res);
        this.data = res;
    }, err => {
      console.log(err);
    })

  }

}

【问题讨论】:

标签: angular typescript ionic2


【解决方案1】:

如果您正在运行 ionic serve 问题是您正在尝试访问不同的域并且您的浏览器阻止了它。您可以将插件添加到 chrome 浏览器以停止阻止。我使用 Allow-Control-Allow-Origin 插件。我希望这会有所帮助。

【讨论】:

  • 但我有一个小小的疑问,当我在手机上运行这个应用程序时,这个问题会出现吗???
猜你喜欢
  • 2017-04-05
  • 2020-08-10
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 2019-03-29
相关资源
最近更新 更多