Javascript 将ajax结果附加到div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29432997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
append ajax result to div
提问by
I'm making an ajax call to the IMDb API to get the movie data for 'The Shawshank Redemption'. I want this data to be put in the div I created.
我正在对 IMDb API 进行 ajax 调用以获取“肖申克的救赎”的电影数据。我希望将这些数据放入我创建的 div 中。
<div id="movie-data"></div>
My js code currently:
我的js代码目前:
$(init);
function init() {
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").append(data);
}
});
It doesn't give any response. However, I can see the data in my console. When I append <p>Test</p>instead of datait does return 'Test' to the screen.
它没有给出任何回应。但是,我可以在控制台中看到数据。当我追加<p>Test</p>而不是data它确实将“测试”返回到屏幕。
回答by
This is what I did. It seems to be working now. Thanks everyone.
这就是我所做的。它现在似乎正在工作。谢谢大家。
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").append(JSON.stringify(data));
回答by Po Po
the answer is:
答案是:
function init() {
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").html($(data).append(data));
}
});
回答by arniotaki
You could try to delete dataType: "json" from your ajax call
您可以尝试从 ajax 调用中删除 dataType: "json"
$.ajax({
dataType: "json",
url: "http://www.omdbapi.com/?i=tt0111161",
success: function (data) {
console.log(data);
$("#movie-data").append(data);
}
});
回答by Palmiro Lousse
You can try with JSON.stringify(data)
您可以尝试使用 JSON.stringify(data)
回答by renakre
The following should work
以下应该工作
$("#movie-data").html(data.Title);
because datawill be in json format, like this:
因为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"}
Check these resources:
检查这些资源:
Using AJAX to Extract Data from IMDB API
http://99webtools.com/blog/php-get-movie-information-from-imdb/
http://99webtools.com/blog/php-get-movie-information-from-imdb/
回答by Raja
Try like this. API is returning JSONvalues you need to get the values like mentioned below. Hope this helps you.
像这样尝试。API 正在返回JSON您需要获取下面提到的值的值。希望这对你有帮助。
var content = 'Title : '+data.Title ;
content += ' Year : '+data.Year ;
content += ' Rated : '+data.Rated ;
content += ' Released : '+data.Released ;
$("#movie-data").append(content);
回答by ozil
<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();
working demo
工作演示

