如何获取此类JSON网址的数据?
http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json?apikey=Removed_My_API_Key“
当我像这样复制并粘贴数据时
var titles = {
"cast": [{
"id": "162664630",
"name": "Sylvester Stallone",
"characters": ["Barney Ross"]
}, {
"id": "162653720",
"name": "Jason Statham",
"characters": ["Lee Christmas"]
}, {
"id": "162652223",
"name": "Jet Li",
"characters": ["Yin Yang"]
}, {
"id": "162664307",
"name": "Dolph Lundgren",
"characters": ["Gunnar Jensen"]
}, {
"id": "162670654",
"name": "Chuck Norris",
"characters": ["Booker"]
}, {
"id": "326392465",
"name": "Terry Crews",
"characters": ["Hale Caesar"]
}, {
"id": "770731413",
"name": "Randy Couture",
"characters": ["Toll Road"]
}, {
"id": "770833479",
"name": "Liam Hemsworth",
"characters": ["Billy the Kid"]
}, {
"id": "770704326",
"name": "Scott Adkins",
"characters": ["Hector"]
}, {
"id": "770670020",
"name": "Nan Yu",
"characters": ["Maggie"]
}, {
"id": "162670708",
"name": "Jean-Claude Van Damme",
"characters": ["Jean Vilain"]
}, {
"id": "162652509",
"name": "Bruce Willis",
"characters": ["Mr. Church"]
}, {
"id": "162662233",
"name": "Arnold Schwarzenegger",
"characters": ["Trench"]
}, {
"id": "489251774",
"name": "Amanda Ooms",
"characters": ["Pilar"]
}, {
"id": "377608335",
"name": "Charisma Carpenter",
"characters": ["Lacy"]
}, {
"id": "771417014",
"name": "Nikolette Noel",
"characters": ["Sophia"]
}],
"links": {
"rel": "http://api.rottentomatoes.com/api/public/v1.0/movies/771238417.json"
}
},
star,
staring = [];
for (star = 0; star < titles.cast.length; star++) {
staring.push(titles.cast[star].name + ' as ' + titles.cast[star].characters[0]);
}
document.getElementById('Credits').innerHTML = staring.join(', ');
它使用上面的代码,我试图找出如何直接从URL本身获取它。
我试过按照方法
var movieUrl = 'http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json?apikey=Removed_My_API'
$(document).ready(function() {
$.ajax({
url: moviesSearchUrl + encodeURI(query),
dataType: "jsonp",
success: searchCallback
});
});
function searchCallback(data) {
var titles = data.cast
star,
staring = [];
for (star = 0; star < titles.cast.length; star++) {
staring.push(titles.cast[star].name + ' as ' + titles.cast[star].characters[0]);
}
document.getElementById('Credits').innerHTML = staring.join(', ');}
使用这个JS小提琴http://jsfiddle.net/6yYYZ/,有人可以帮忙吗?
答案 0 :(得分:2)
你的代码中有很多错误,我部分修复它以便你获得数据
最重要的修复是你的ajax调用中的url是错误的,我解决了这个问题。
var movieUrl = 'http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json?apikey=API_KEY'
$(document).ready(function() {
$.ajax({
url: movieUrl,
dataType: "jsonp",
success: searchCallback
});
});
function searchCallback(data) {
console.log(data); // do stuff with your json here
}
答案 1 :(得分:1)
titles = data.cast
,因为稍后您引用titles.cast.length
...因为您已将titles
设置为cast
的值,.cast
1}}出现null
,然后对其.length
执行错误输出。
只需设置titles = data
(与复制和粘贴数据时相同)即可,因为现在存在.cast
。这是我的小提琴(因为apiKey已删除,但我怀疑这是否是你的公钥并且无论如何都会在你的任何公共网页上都可见)。
var movieUrl = 'http://api.rottentomatoes.com/api/public/v1.0/movies/770672122/cast.json?apikey=Removed_My_API'
$(document).ready(function() {
$.ajax({
url: movieUrl, //this was wrong
dataType: "jsonp",
success: searchCallback
});
});
function searchCallback(data) {
var titles = data, //just set it to data
star = 0, //initialize star
staring = [];
for (star = 0; star < titles.cast.length; star++) {
staring.push(titles.cast[star].name + ' as ' + titles.cast[star].characters[0]);
}
document.getElementById('Credits').innerHTML = staring.join(', ');
}