如何解决在API中获取数据的问题
遇到这个问题的人从我的api中检索数据。假设api响应是这样的: 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form id="form" onsubmit="return false;">
<input type="text" id="myText" value="chefbezos" />
<button onclick="myFunction()">Try it</button>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.0/jquery.min.js"></script>
<script>
var username = document.getElementById('myText').value;
$.getJSON({
url : 'https://fortnite-api.com/v1/stats/br/v2?name=ninja',headers: {'Authorization': '5d661c2a-59e41e68-854c5a46-6f742ef8'},success : function(data) {
console.log(data.data.account.name)
}
});
</script>
</body>
</html>
结果:
fortnite.html:22 Uncaught TypeError: Cannot read property 'account' of undefined
at Object.success (fortnite.html:22)
at c (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at l (jquery.min.js:2)
at XMLHttpRequest.<anonymous> (jquery.min.js:2)
API响应:
{"status":200,"data":{"account":{"id":"4735ce9132924caf8a5b17789b40f79c","name":"Ninja"},"battlePass":{"level":91,"progress":57},"image":null,"stats":{"all":{"overall":{"score":3276391,"scorePerMin":34.885,"scorePerMatch":149.709,"wins":7582,"top3":3289,"top5":3504,"top6":3871,"top10":3168,"top12":4329,"top25":4004,"kills":133467,"killsPerMin":1.421,"killsPerMatch":6.099,"deaths":14303,"kd":9.331,"matches":21885,"winRate":34.645,"minutesPlayed":93920,"playersOutlived":528192,"lastModified":"2020-10-13T02:11:41Z"},"solo":{"score":703737,"scorePerMin":34.416,"scorePerMatch":95.851,"wins":2195,"top10":3116,"top25":3944,"kills":47759,"killsPerMin":2.336,"killsPerMatch":6.505,"deaths":5147,"kd":9.279,"matches":7342,"winRate":29.896,"minutesPlayed":20448,"playersOutlived":129606,"duo":{"score":1270669,"scorePerMin":32.861,"scorePerMatch":171.875,"wins":2684,"top5":3477,"top12":4293,"kills":42475,"killsPerMin":1.098,"killsPerMatch":5.745,"deaths":4709,"kd":9.02,"matches":7393,"winRate":36.305,"minutesPlayed":38668,"playersOutlived":204404,"lastModified":"2020-10-02T23:04:37Z"},"trio":{"score":47378,"scorePerMin":26.587,"scorePerMatch":301.771,"wins":24,"top3":28,"top6":53,"kills":491,"killsPerMin":0.276,"killsPerMatch":3.127,"deaths":133,"kd":3.692,"matches":157,"winRate":15.287,"minutesPlayed":1782,"playersOutlived":7930,"lastModified":"2020-10-06T19:34:56Z"},"squad":{"score":1254607,"scorePerMin":37.993,"scorePerMatch":179.409,"wins":2679,"top3":3161,"top6":3707,"kills":42742,"killsPerMin":1.294,"killsPerMatch":6.112,"deaths":4314,"kd":9.908,"matches":6993,"winRate":38.31,"minutesPlayed":33022,"playersOutlived":186252,"lastModified":"2020-10-03T00:42:42Z"},"ltm":{"score":115225,"scorePerMin":42.55,"scorePerMatch":286.629,"wins":157,"kills":3128,"killsPerMin":1.155,"killsPerMatch":7.781,"deaths":245,"kd":12.767,"matches":402,"winRate":39.055,"minutesPlayed":2708,"playersOutlived":14193,"lastModified":"2020-04-03T06:37:53Z"}},"keyboardMouse":{"overall":{"score":3264456,"scorePerMin":34.884,"scorePerMatch":149.574,"wins":7575,"top3":3287,"top5":3498,"top6":3869,"top10":3165,"top12":4318,"top25":3996,"kills":133285,"killsPerMin":1.424,"killsPerMatch":6.107,"deaths":14250,"kd":9.353,"matches":21825,"winRate":34.708,"minutesPlayed":93580,"playersOutlived":525777,"solo":{"score":699630,"scorePerMin":34.429,"scorePerMatch":95.827,"wins":2194,"top10":3113,"top25":3936,"kills":47699,"killsPerMin":2.347,"killsPerMatch":6.533,"deaths":5107,"kd":9.34,"matches":7301,"winRate":30.051,"minutesPlayed":20321,"playersOutlived":128479,"duo":{"score":1264352,"scorePerMin":32.85,"scorePerMatch":171.391,"wins":2680,"top5":3471,"top12":4282,"kills":42384,"killsPerMin":1.101,"deaths":4697,"kd":9.024,"matches":7377,"winRate":36.329,"minutesPlayed":38489,"playersOutlived":203319,"squad":{"score":1253096,"scorePerMin":37.986,"scorePerMatch":179.27,"wins":2677,"top3":3159,"top6":3705,"kills":42711,"killsPerMin":1.295,"killsPerMatch":6.11,"deaths":4313,"kd":9.903,"matches":6990,"winRate":38.298,"minutesPlayed":32988,"playersOutlived":186049,"ltm":{"score":114074,"scorePerMin":42.837,"scorePerMatch":285.9,"kills":3060,"killsPerMin":1.149,"killsPerMatch":7.669,"deaths":242,"kd":12.645,"matches":399,"winRate":39.348,"minutesPlayed":2663,"playersOutlived":14084,"lastModified":"2020-02-17T20:41:05Z"}},"gamepad":{"overall":{"score":11719,"scorePerMin":35.948,"scorePerMatch":285.829,"wins":7,"top3":2,"top5":6,"top6":2,"top10":3,"top12":11,"top25":5,"kills":178,"killsPerMin":0.546,"killsPerMatch":4.341,"deaths":34,"kd":5.235,"matches":41,"winRate":17.073,"minutesPlayed":326,"playersOutlived":2261,"lastModified":"2020-07-08T17:15:40Z"},"solo":{"score":3891,"scorePerMin":34.434,"scorePerMatch":176.864,"wins":1,"kills":56,"killsPerMin":0.496,"killsPerMatch":2.545,"deaths":21,"kd":2.667,"matches":22,"winRate":4.545,"minutesPlayed":113,"playersOutlived":973,"duo":{"score":6317,"scorePerMin":35.291,"scorePerMatch":394.812,"wins":4,"kills":91,"killsPerMin":0.508,"killsPerMatch":5.688,"deaths":12,"kd":7.583,"matches":16,"winRate":25,"minutesPlayed":179,"playersOutlived":1085,"lastModified":"2020-04-23T22:16:27Z"},"trio":null,"squad":{"score":1511,"scorePerMin":44.441,"scorePerMatch":503.667,"wins":2,"kills":31,"killsPerMin":0.912,"killsPerMatch":10.333,"deaths":1,"kd":31,"matches":3,"winRate":66.667,"minutesPlayed":34,"playersOutlived":203,"lastModified":"2019-06-04T23:55:25Z"},"ltm":{"score":1151,"scorePerMin":25.578,"scorePerMatch":383.667,"wins":0,"kills":68,"killsPerMin":1.511,"killsPerMatch":22.667,"deaths":3,"kd":22.667,"winRate":0,"minutesPlayed":45,"playersOutlived":109,"touch":{"overall":{"score":216,"scorePerMin":15.429,"scorePerMatch":11.368,"top3":0,"top5":0,"top6":0,"top10":0,"top12":0,"top25":3,"kills":4,"killsPerMin":0.286,"killsPerMatch":0.211,"deaths":19,"kd":0.211,"matches":19,"minutesPlayed":14,"playersOutlived":154,"lastModified":"2018-12-24T03:00:08Z"},"solo":{"score":216,"duo":null,"squad":null,"ltm":null}}}}
用户名的数据不应该为data.data.account.name
吗?当我放入它时,它不会接收任何数据。上方的数据链接正确吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。