获取奇迹数据图像错误

时间:2017-08-12 17:46:28

标签: javascript api

这是js

//Marvel Comics API
var main = "http://gateway.marvel.com/v1/public/characters?name=";
var input = "doctor strange";
var ts = "&ts=1";
var apikey = "&apikey=my api";
var hash = "&hash=my hash";

function setup(){
//input = document.getElementById('search')
document.getElementById('submit').addEventListener('click', loadData);
}
function loadData(){
    var url = main+input+ts+apikey+hash;
    loadJSON(url, gotData)
}
function gotData(data){
    document.getElementById('img').src = data.results.thumbnail.path+".jpg";
}

这是html

<html>
<head>
  <meta charset="UTF-8">
  <script language="javascript" type="text/javascript" src="libraries/p5.js"></script>
  <script language="javascript" src="libraries/p5.dom.js"></script>
  <script language="javascript" type="text/javascript" src="myScript.js"></script>
  <!-- <script language="javascript" type="text/javascript" src="codepen.js"></script> -->

</head>

<body>
        <img id="img" src="" alt="imageHere" width="500" height="500"/>
    <button id="submit">Submit</button>
</body>
</html>

问题在于每当我按下提交btton它应该加载img但它不工作它说“

Uncaught (in promise) TypeError: Cannot read property 'thumbnail' of undefined
    at gotData (myScript.js:21)
    at p5.js:22613
    at p5.js:23297
    at <anonymous>
gotData @ myScript.js:21
(anonymous) @ p5.js:22613
(anonymous) @ p5.js:23297
Promise rejected (async)
p5.httpDo @ p5.js:23298
p5.loadJSON @ p5.js:22608
loadData @ myScript.js:18"
请帮帮我...我查了json数据流其正确无法搞清楚,谢谢。

json ......在这里

"code": 200,
"status": "Ok",
"copyright": "© 2017 MARVEL",
"attributionText": "Data provided by Marvel. © 2017 MARVEL",
"attributionHTML": "<a href=\"http://marvel.com\">Data provided by Marvel. © 2017 MARVEL</a>",
"etag": "daaf602f2f234c3378f09acdb27921a071b0aef4",
"data": {
"offset": 0,
"limit": 20,
"total": 1,
"count": 1,
"results": [
{
"id": 1009282,
"name": "Doctor Strange",
"description": "",
"modified": "2016-09-28T12:03:08-0400",
"thumbnail": {
"path": "http://i.annihil.us/u/prod/marvel/i/mg/5/f0/5261a85a501fe",
"extension": "jpg"
}

这不是总数,而是我试图获取的json ......

0 个答案:

没有答案