【问题标题】:Dealing with text/plain in iron-ajax and dom-repeat在 Iron-ajax 和 dom-repeat 中处理 text/plain
【发布时间】:2018-02-14 16:05:07
【问题描述】:

我有一个 API 不幸返回 text/plain 元素。虽然输出格式是 JSON,但 mime 类型是text/plain

当使用iron-ajax 从API 请求输出时,我可以将输出存储在一个名为response 的字符串中。但是,我无法遍历输出,这是一个类似 json 对象的列表

[ {"userid": 1, "name": "testuser"}, {"userid": 2, "name": "testuser 2"}]

因为 dom-repeat 抱怨这个对象不是一个数组。

我尝试使用某些附加功能进行 dom-repeat,例如:

<template is="dom-repeat" items="{{response.values()}}">

<template is="dom-repeat" items="{{Array.from(response)}}">

但是这些函数什么也不返回。如何在 dom-repeat 中处理我的文本/纯文本数据?

【问题讨论】:

    标签: javascript polymer iron-ajax


    【解决方案1】:

    如果您收到String 格式的响应,您可以转换为JSON.Parse(string),并且您在上面提供的格式正是 dom-repeat 需要的格式。

    这里是下面的链接,您可以查看。 (你甚至可以调试控制台)

    DEMO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 1970-01-01
      相关资源
      最近更新 更多