我正在对IMDb API进行ajax调用,以获取Shawshank Redemption'的电影数据。我希望将这些数据放入我创建的div中。
<div id="movie-data"></div>
目前我的js代码:
$(init);
function init() {
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").append(data);
}
});
它没有给出任何回应。但是,我可以在控制台中看到数据。当我追加<p>Test</p>
而不是data
时,它会返回&#39;测试&#39;到屏幕。
答案 0 :(得分:2)
这就是我所做的。它似乎现在正在运作。感谢大家。
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").append(JSON.stringify(data));
答案 1 :(得分:0)
以下内容应该有效
$("#movie-data").html(data.Title);
因为data
将采用json格式,如下所示:
{"Title":"Titanic","Year":"1997","Rated":"PG-13","Released":"19 Dec 1997","Runtime":"3 h 14 min","Genre":"Drama, Romance","Director":"James Cameron","Writer":"James Cameron","Actors":"Leonardo DiCaprio, Kate Winslet, Billy Zane, Kathy Bates","Plot":"A seventeen-year-old aristocrat, expecting to be married to a rich claimant by her mother, falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.","Poster":"http://ia.media-imdb.com/images/M/MV5BMjExNzM0NDM0N15BMl5BanBnXkFtZTcwMzkxOTUwNw@@._V1_SX300.jpg","imdbRating":"7.6","imdbVotes":"449,162","imdbID":"tt0120338","Type":"movie","Response":"True"}
检查这些资源:
Using AJAX to Extract Data from IMDB API
http://99webtools.com/blog/php-get-movie-information-from-imdb/
答案 2 :(得分:0)
试试这样。 API返回您需要的JSON
值,以获取如下所述的值。希望这会对你有所帮助。
var content = 'Title : '+data.Title ;
content += ' Year : '+data.Year ;
content += ' Rated : '+data.Rated ;
content += ' Released : '+data.Released ;
$("#movie-data").append(content);
答案 3 :(得分:0)
<div id="movie-data"></div>
function init() {
var html='';
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
for(var key in data) {
var value = data[key];
html+='<div>'+key+':'+value+'</div>'
}
$("#movie-data").append(html);
}
});
}
init();
工作demo
答案 4 :(得分:0)
答案是:
function init() {
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").html($(data).append(data));
}
});
答案 5 :(得分:0)
您可以尝试从ajax调用中删除dataType:“json”
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").append(data);
}
});
答案 6 :(得分:0)
您可以尝试使用JSON.stringify(数据)
答案 7 :(得分:0)
代码如下:
!