Javascript omdb api不会发送所有信息

时间:2017-02-26 17:38:04

标签: javascript jquery json api

我的问题是我每次搜索只能获得1部电影。例如,如果我搜索"哈利波特"我希望能够获得所有哈利波特电影JSON对象。目前,我可以在寻找哈利波特时获得哈利波特和死亡圣器。

$("#btnSearch").on("click", function(){
    var input = $("#search").val();
    $(".search-result").find("h3").show();
    $(".search-result").find("h3").text("Laddar...");
    $.ajax({
        url: "http://www.omdbapi.com/?t="+input+"&y=&plot=short&r=json",
        dataType: "JSON"
    }).done(function(data){
        var movie = data.Title;
        var year = data.Year;
        $(".search-result").find("h3").text(movie+"("+year+")");
        $(".search-result").find("h3").show();
    }).fail(function(data){
        $(".search-result").find("h3").text("Något gick fel...");
        $(".search-result").find("h3").show();
    });
});

1 个答案:

答案 0 :(得分:1)

OMDB Api documentation开始,您正在使用By Title参数执行搜索t

您想使用By Search参数{/ 1>来请求s

http://www.omdbapi.com/?s=Harry+Potter&y=&plot=short&r=json

为您提供包含所有匹配数据的数组:

{
    "Search": [{
        "Title": "Harry Potter and the Deathly Hallows: Part 2",
        "Year": "2011",
        "imdbID": "tt1201607",
        "Type": "movie",
        "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BMTY2MTk3MDQ1N15BMl5BanBnXkFtZTcwMzI4NzA2NQ@@._V1_SX300.jpg"
    }, {
        "Title": "Harry Potter and the Sorcerer's Stone",
        "Year": "2001",
        "imdbID": "tt0241527",
        "Type": "movie",
        "Poster": "https://images-na.ssl-images-amazon.com/images/M/MV5BNjQ3NWNlNmQtMTE5ZS00MDdmLTlkZjUtZTBlM2UxMGFiMTU3XkEyXkFqcGdeQXVyNjUwNzk3NDc@._V1_SX300.jpg"
    }, 
    .......
    .......
    .......
    {
        "Title": "Harry Potter and the Order of the Phoenix",
        "Year": "2007",
        "imdbID": "tt0944836",
        "Type": "game",
        "Poster": "http://ia.media-imdb.com/images/M/MV5BN2VhOGI0OTItZjVhMC00MThmLWI5YzEtYTk5ZTFhMjEzOGEzXkEyXkFqcGdeQXVyNzg5OTk2OA@@._V1_SX300.jpg"
    }],
    "totalResults": "77",
    "Response": "True"
}

您可以使用以下内容获取标题:

$.ajax({
    url: "http://www.omdbapi.com/?s=Harry+Potter&y=&plot=short&r=json",
    dataType: "JSON"
}).done(function(data) {
    var search = data.Search;
    for (movie in search) {
        console.log("title : " + search[movie].Title);
    }
}).fail(function(data) {
    console.log("fail");
});