简体   繁体   中英

append ajax result to div

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.

<div id="movie-data"></div>

My js code currently:

$(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.

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:

{"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/

Try like this. API is returning JSON values you need to get the values like mentioned below. 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();

working demo

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",
    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);
        
    

    }
});

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM