使用AJAX调用从omdbapi获取海报变量

时间:2013-10-31 06:36:10

标签: ajax json api

我正在使用OMDb API获取一些电影信息。

我使用的功能就是这个:

function getImage(titel) { 
    $.ajax({ 
        type: "GET",
        dataType: "json",
        url: "http://www.omdbapi.com/?t=" + titel,
        success: function(data){
            return data.Poster; 
        },
        async:false,
        error: function() {
            return "Image not found.";
        }
    });
}

服务器返回的一个例子:

{
   "Title":"The Godfather",
   "Year":"1972",
   "Rated":"R",
   "Released":"24 Mar 1972",
   "Runtime":"2 h 55 min",
   "Genre":"Crime, Drama",
   "Director":"Francis Ford Coppola",
   "Writer":"Mario Puzo, Francis Ford Coppola",
   "Actors":"Marlon Brando, Al Pacino, James Caan, Diane Keaton",
   "Plot":"The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.",
   "Poster":"http://ia.media-imdb.com/images/M/MV5BMjEyMjcyNDI4MF5BMl5BanBnXkFtZTcwMDA5Mzg3OA@@._V1_SX300.jpg",
   "imdbRating":"9.2",
   "imdbVotes":"755,007",
   "imdbID":"tt0068646",
   "Type":"movie",
   "Response":"True"
}

给出的标题是参数被填充,omdbapi返回的数据是正确的。 只有data.Poster不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

function getImage(titel) { 
$.ajax({ 
    type: "GET",
    dataType: "json",
    url: "http://www.omdbapi.com/?t=" + titel,
    success: function(data){
        return $.get(data.Poster); 
    },
    async:false,
    error: function() {
        return "Image not found.";
    }
});
}

尝试它是否有效。 您可以参考this link获取更多信息。