【问题标题】:Ionic 3 Error in JSON ParsingJSON解析中的Ionic 3错误
【发布时间】: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 问题
  • 谢谢@suraj,正如你所说,这是 CORS 问题。但我通过在 Google Chrome 中添加 chrome 扩展“允许控制允许来源”来解决它

标签: javascript json parsing typescript ionic-framework


【解决方案1】:

我通过添加 Google Chrome 扩展 Access-Control-Allow-Origin 找到了 CORS 问题的解决方案。但是在我们每次打开浏览器都必须启用它之前

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 2018-02-25
    • 2018-10-27
    • 2016-09-10
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多