【问题标题】:how to consume response object in ionic2如何在ionic2中使用响应对象
【发布时间】:2017-07-16 11:52:32
【问题描述】:

我正在使用ionic2,我正在使用http post请求从webservice获取数据,返回的数据是:

响应 {_body:"{"Status":{"list all categories":[{"id":9,"name_en…18}],"message":["All categories."],"version" :27}}”,状态:200,ok:true,statusText:“OK”,标题:标题...} 标题 : 标头 好的 : 真的 地位 : 200 状态文本 : “好的” 类型 : 2 网址 : "http://safe-pay.co/safepay/public/api/user" _身体 : "{"状态":{"列出所有类别":[{"id":9,"name_en":"电信","name_ar":"\u0627\u062a\u0635\u0627\u0644\u0627\u062a", “图片”:“http://safe-pay.co/safepay/public/uploadedimg/categories/5937ec7b75164.PNG”,“描述”:“电信”,“cat_enet_id”:1},{“id”:7, "name_en":"在线优惠券","name_ar":"\u0628\u0637\u0627\u0642\u0627\u062a \u062a\u0633\u0648\u0642 \u0639\u0628\u0631 \u0627\u0644\u0627\u0646\u06 \u0631\u0646\u062a","图片":"http://safe-pay.co/safepay/public/uploadedimg/categories/5937f109a226b.png","description":"在线优惠券","cat_enet_id":5 },{"id":8,"name_en":"电视频道","name_ar":"\u0645\u062d\u0637\u0627\u062a \u062a\u0644\u0641\u0632\u064a\u0648\u0646\u064a\ u0629","image":"http://safe-pay.co/safepay/public/uploadedimg/categories/5937ecb48cef3.PNG","description":"电视频道","cat_enet_id":6},{"id ":10,"name_en":"数字游戏","name_ar":"\u0627\u0644\u0639\u0627\u0628\u0627\u0646\u062a\u0631\u0646\u062a","图片":"http:// /safe-pay.co/safepay/public/uploadedimg/categories/5937 eccc27ce8.png","description":"游戏","cat_enet_id":17},{"id":11,"name_en":"娱乐","name_ar":"\u0627\u0644\u062a\u0631\u0641 \u064a\u0647 \u0644\u0644\u0623\u0637\u0641\u0627\u0644","图片":"http://safe-pay.co/safepay/public/uploadedimg/categories/5937f5ae9​​7fe6.png","说明":"Games","cat_enet_id":18}],"message":["所有类别。"],"version":27}}" 原型 : 正文

这是函数:

getCategories() {
let objData = {"Data":{
        "action": "CATEGORIES",
        "lang": "AR"
    },
    "Request": {

    }
}

var headers = new Headers();
headers.append('Content-Type', 'application/json');

this.http.post(this.userUrl, objData, {headers:headers})
    .subscribe(data => {
        this.categories = data
        console.log("categories array ",this.categories," data ",Promise.resolve(data));
    }, error => {
        console.log("Oooops! ",error);
    });
}

如何从数组中获取数据

【问题讨论】:

    标签: angular ionic2 angular2-services


    【解决方案1】:

    您应该将其映射为json() 格式

    this.http.post(this.userUrl, objData, {headers:headers})
             .map(response=><T>response.json())
             ...
    

    注意:需要导入地图操作符,可以从

    import 'rxjs/add/operator/map';
    

    T 想要将 json() 转换成的类型。

    【讨论】:

    • @Al-shimaaAdel 很乐意为您提供帮助。 :) 需要更多帮助或您的明确吗?
    • 非常感谢,我得到了我想要的数据:(5) [Object, Object, Object, Object, Object] 但是如果我在提供者中订阅,如果我订阅,则找不到其他数据在页面中我进入请求的无限循环
    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 2017-08-29
    • 2017-06-07
    • 2021-03-31
    相关资源
    最近更新 更多