【发布时间】: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);
})
}
}
【问题讨论】:
-
您遇到了哪个错误?
-
XMLHttpRequest 无法加载 example.com/jsondata 。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'localhost:8100'。
-
你能用你的json结构更新你的问题吗?
-
@MohanGopi 正确的 url ??
标签: angular typescript ionic2