通过设置Ionic-Cli代理解决ionic serve跨域调试问题
Ionic-Cli代理设置:
打开ionic.config.json文件,添加proxies代理配置字段:
{
"name": "ion",
"app_id": "",
"v2": true,
"typescript": true,
"proxies": [
{
"path": "/web",
"proxyUrl": "http://127.0.0.1/phpResty/web"
}
]
}
之后在provider中这样调用即可:
url:string = "/web/";
api(){
return new Promise((resolve, reject)=>{
this.http.get(this.url + "api.php").subscribe(res => {
resolve(res.json)
}, err => {
reject(err);
});
});
}
此时访问 http://localhost:8100/web/api.php 等同于访问 http://127.0.0.1/phpResty/web/api.php
从而避开了因端口不同而产生的跨域问题。
参考:http://www.jianshu.com/p/e9c85dbf406d
http://blog.csdn.net/haozhoupan/article/details/51149896