gpt4 book ai didi

javascript - JSON回调问题

转载 作者:行者123 更新时间:2023-11-28 20:02:56 25 4
gpt4 key购买 nike

如何获取像这样的 JSON url 数据?

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 Fiddle http://jsfiddle.net/6yYYZ/ ,有人可以帮忙吗?

最佳答案

您的代码中有很多错误,我部分修复了它,以便您取回数据

最重要的修复是你的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

}

http://jsfiddle.net/6yYYZ/5/

关于javascript - JSON回调问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21314087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com