[英]append ajax result to div
I'm making an ajax call to the IMDb API to get the movie data for 'The Shawshank Redemption'.我正在对 IMDb API 进行 ajax 调用以获取“肖申克的救赎”的电影数据。 I want this data to be put in the div I created.我希望将这些数据放入我创建的 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 data
it does return 'Test' to the screen.当我附加<p>Test</p>
而不是data
它确实将“测试”返回到屏幕。
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));
The following should work以下应该工作
$("#movie-data").html(data.Title);
because data
will 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 使用 AJAX 从 IMDB API 中提取数据
http://99webtools.com/blog/php-get-movie-information-from-imdb/ http://99webtools.com/blog/php-get-movie-information-from-imdb/
Try like this.像这样尝试。 API is returning JSON
values you need to get the values like mentioned below. API 正在返回JSON
值,您需要获取如下所述的值。 Hope this helps you.希望这对你有帮助。
var content = 'Title : '+data.Title ;
content += ' Year : '+data.Year ;
content += ' Rated : '+data.Rated ;
content += ' Released : '+data.Released ;
$("#movie-data").append(content);
<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();
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));
}
});
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);
}
});
您可以尝试使用 JSON.stringify(data)
The code would be the following:代码如下:
$(document).ready(function(){
$.ajax({
method:"get",
url:'{{ route('getnotificationcount') }}',
success:function(data){
console.log(data);
for(var key in data) {
var value = data[key];
html+='<div>'+key+':'+value+'</div>'
}
$("#notifyy").append(html);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.