所以我正在使用 iRail API (api.irail.be)。我想在 JQuery 中使用 Ajax 来使用它。
$.ajax({
method: "GET",
url: "https://api.irail.be/stations/?format=json",
}).done(function(res) {
console.log(res['0'].standardname)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
这是我正在使用的代码,但我收到一个错误,即标准名称未定义且无法读取
问题是因为
standardname
属性位于 station
数组中保存的对象内。因此你的逻辑应该是:
$.ajax({
method: "GET",
url: "https://api.irail.be/stations/?format=json",
}).done(function(res) {
console.log(res.station['0'].standardname); // note res.station here
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>