如何使用和服API

时间:2014-03-20 00:13:17

标签: json api

目标:使用jquery解析JSON API数据,以便在样式列表中显示静态(无后端)网站上的信息。

我使用和服创建了一个API,现在需要显示这些信息。对于从何处入手的任何帮助都会很棒。

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
 function kimonoCallback(data) {
    // do something with the data
  }

   $.ajax({
     "url":"http://www.kimonolabs.com/api/42ts6px8?apikey=363e7e1d3fffb45d424ad535ebdc233d&callback=kimonoCallback",
     "crossDomain":true,
     "dataType":"jsonp"
   });
</script>

示例JSON数据:

  {
  "name": "HN",
  "count": 30,
  "frequency": "daily",
  "version": 1,
  "newdata": true,
  "lastrunstatus": "success",
  "lastsuccess": "Wed Mar 19 2014 23:23:40 GMT+0000 (UTC)",
  "nextrun": "Thu Mar 20 2014 23:23:40 GMT+0000 (UTC)",
  "results": {
  "collection1": [
  {
    "property1": {
      "href": "http://blog.samaltman.com/new-rfs-breakthrough-technologies",
      "text": "New RFS – Breakthrough Technologies"
    },
    "source": "(samaltman.com)"
  },
  {
    "property1": {
      "href": "https://www.unrealengine.com/blog/welcome-to-unreal-engine-4",
      "text": "Welcome to Unreal Engine 4"
    },
    "source": "(unrealengine.com)"
  },
  {
    "property1": {
      "href": "https://www.crypto101.io/",
      "text": "Crypto 101"
    },
    "source": "(crypto101.io)"
  },
  {
    "property1": {
      "href": "http://sivers.org/full",
      "text": "What if you didn't need money or attention?"
    },
    "source": "(sivers.org)"
  }

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

当您在dataType:'jsonp'中指定$.ajax()属性时,不需要解析JSON,jQuery将自动解析来自服务器的响应并将解析后的JSON对象传递给成功回调{{1} },如果仅从kimonoCallback(data)来电中删除dataType:"jsonp"JSON.parse()jQuery.parseJSON() ...等),则需要解析JSON。

$.ajax()