2013-10-31 38 views
0

我使用OMDb API獲取一些影片信息。使用AJAX調用從omdbapi獲取海報變量

我使用的功能是這個:

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/[email protected]@._V1_SX300.jpg", 
    "imdbRating":"9.2", 
    "imdbVotes":"755,007", 
    "imdbID":"tt0068646", 
    "Type":"movie", 
    "Response":"True" 
} 

給出的影片名稱的參數被填滿,並通過omdbapi返回的數據是正確的。 只有data.Poster不起作用。我究竟做錯了什麼?

+0

您不能從'success'和'failure'返回數據,因爲這些功能在主循環中不會執行。如何做Ajax調用:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call –

回答

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以獲取更多信息。