0

I am trying to get IMDB info using JavaScript. The following code is working for Title, Year, IMDB link but when I am trying to get "Runtime", "Director", "Released" etc I am getting as undefined. Please help!

This is the data formate: https://www.omdbapi.com/?i=tt4779682&apikey=ba1f4581

Code:

var data;
function getanswer(q){
$.get("https://www.omdbapi.com/?s="+q+"&apikey=ba1f4581", function(rawdata){
var rawstring =JSON.stringify(rawdata);
data =JSON.parse(rawstring);
var title = data.Search[0].Title;
var year = data.Search[0].Year; 
var director = data.Search[0].Director; 
var imdburl="https://www.imdb.com/title/"+data.Search[0].imdbID+"/";

var posterurl =data.Search[0].Poster;
document.getElementById('answer').innerHTML="<h1>"+title+"</h1><br> <img src= '"+posterurl+"'><br><p> Year Released:"+year+"</p> <br><p> Year Released:"+director+"</p> <br> <p> IMDB page: <a href='"+imdburl+"'target='_blank'>"+imdburl+"</a></p>"; }); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<html>
<head></head>
<body onload="getanswer(document.getElementById('querybox').value)">
<!doctype html> <input value="The Meg" onkeyup="getanswer(document.getElementById('querybox').value)" id="querybox"> <div id="answer"></div> 
</body>
</html>
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
santosh
  • 742
  • 8
  • 19
  • Do a `console.log(data)` and you'll see that the data doesn't contain the information you want. You might have to consult the API documentation for how to include the data you want. Also immediately calling `JSON.parse` on the result of `JSON.stringify` is usually unnecessary (unless you want to clone the data?). You can just access `rawdata` directly. `data` and `rawdata` are both objects and have the same structure. – Felix Kling Oct 20 '21 at 22:45
  • It could very well be that you'll have to make a second request using the `imdbID` value to get the detailed information. – Felix Kling Oct 20 '21 at 22:48

1 Answers1

1

According to the api documentation, you were using s parameter that would not return Runtime/Director/etc. You could use the t parameter and remove Search[0].

Below is the code that would work:

var data;
function getanswer(q){
$.get("https://www.omdbapi.com/?t="+q+"&apikey=ba1f4581", function(rawdata){
var rawstring =JSON.stringify(rawdata);
data =JSON.parse(rawstring);
console.log(data);
var title = data.Title;
var year = data.Year; 
var director = data.Director; 
var imdburl="https://www.imdb.com/title/"+data.imdbID+"/";

var posterurl =data.Poster;
document.getElementById('answer').innerHTML="<h1>"+title+"</h1><br> <img src= '"+posterurl+"'><br><p> Year Released:"+year+"</p> <br><p> Year Released:"+director+"</p> <br> <p> IMDB page: <a href='"+imdburl+"'target='_blank'>"+imdburl+"</a></p>"; }); }
llo
  • 136
  • 7
  • What is the "s" parameter what should I add instead of "Search[0]". Please implement the code and show me. It will be big favor, sir! – santosh Oct 20 '21 at 23:43
  • 1
    I have added the code. You may want to refer to the Parameters section of [OMDb API](https://www.omdbapi.com/). – llo Oct 20 '21 at 23:59