JSON解析中的Ionic 3错误
Posted
技术标签:
【中文标题】JSON解析中的Ionic 3错误【英文标题】:Ionic 3 Error in JSON Parsing 【发布时间】:2017-11-22 13:33:05 【问题描述】:在 Ionic 中,我在尝试解析 JSON 数据时遇到此错误。
Error : Response with status: 0 for URL: null
我需要以列表格式打印产品名称(姜黄粉)
JSON 响应是
["product_id":"40","sku":"HOS001","name":"Turmeric Powder","set":"9","type":"configurable","category_ids":["2","3"],"website_ids":["1"],"product_id":"41","sku":"HOS001-20 grms","name":"Turmeric Powder-20 grms","set":"9","type":"simple","category_ids":[],"website_ids":["1"]]
输入脚本代码:
getdata()
this.loading.present();
this.httpProvider.getJsonData().subscribe(
result =>
console.log('getJsonData completed');
this.newsData=result.data.result;
console.log("Success : "+this.newsData);
,
err =>
console.error("Error : "+err);
,
() =>
this.loading.dismiss();
console.log('getData completed');
);
Provider.ts
import Injectable from '@angular/core';
import Http from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the HttpProvider provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class HttpProvider
constructor(public http: Http)
console.log('Hello HttpProvider Provider');
getJsonData()
return this.http.get('http://happyos.in/prd.php').map(res => res.json());
【问题讨论】:
你在使用ionic serve
吗?这可能是 CORS 问题
@suraj 是的,我正在使用 ionic serve 在 Firefox 浏览器中运行它
@suraj 你能建议如何解决 CORS 问题
一种方法是***.com/a/37779476/4826457
谢谢@suraj,正如你所说,这是 CORS 问题。但我通过在 Google Chrome 中添加 chrome 扩展“允许控制允许来源”来解决它
【参考方案1】:
我通过添加 Google Chrome 扩展 Access-Control-Allow-Origin 找到了 CORS 问题的解决方案。但是在我们每次打开浏览器都必须启用它之前
【讨论】:
以上是关于JSON解析中的Ionic 3错误的主要内容,如果未能解决你的问题,请参考以下文章