【问题标题】:Accessing valid JSON array after sending JSONP request? (Polymer)发送 JSONP 请求后访问有效的 JSON 数组? (聚合物)
【发布时间】:2016-12-30 16:00:37
【问题描述】:

又回来了。为了解决 CORS 问题,我现在使用替代的 Polymer 元素 (byutv-jsonp) 而不是 iron-ajax。如您所见,它成功返回了有效的 JSON 响应,但现在我似乎无法访问它,并且有一个语法错误让我认为我需要将字符串化回 JSON...?不太确定。

  <template is="dom-bind">

    <byutv-jsonp  
      auto 
      url="https://api.bookeo.com/v2/bookings/" 
      params='
        {
        "secretKey":"xxxxxxxxxxx", 
        "apiKey": "xxxxxxxxxxxxxxx" ,
        "startTime": "2016-08-10T00:00:00.000Z",
        "endTime": "2016-08-16T00:00:00.000Z"
        }' 
       last-response="{{lastResponse}}"
       on-response="handleResponse"
       debounce-duration="300"></byutv-jsonp>

    <template is="dom-repeat" items="{{lastResponse}}">
      <div>{{item.title}}</div>
    </template>

  </template>

【问题讨论】:

  • lastResponse 设置在哪里?看起来您正在将最后一个回复设置为 items。哪个,我看不到在任何地方被使用。
  • @Anthony 好的,我想我在最后的响应中有一个错误,我在上面修复了,所以现在我的代码与官方的 demo(第 60 行)匹配,但仍然具有相同的问题
  • 响应不是有效的 JSONP。看起来 bookeo 不支持 JSONP。 bookeo.com/api/protocol 没有这个支持 byutv-jsonp 将无法工作。

标签: json ajax polymer jsonp


【解决方案1】:

问题在于您的 json 数据。您正在尝试访问对象的title,但它嵌套在数组和父对象中。这应该基于您显示的 json 对象工作

<template is="dom-repeat" items="{{lastResponse.data[0]}}">
  <div>{{item.title}}</div>
</template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2016-04-28
    • 2016-05-30
    • 2016-10-14
    • 2017-09-18
    相关资源
    最近更新 更多