0

why is this working?

var movieName = encodeURI("deadpool");
var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + movieName;

and this not?; iven put a console log to check and that works

var movieName = $(".shown .title").html();
console.log(movieName);

var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + encodeURI(movieName);
drunkgummyboy
  • 15
  • 1
  • 5

1 Answers1

0

The HTML may have whitespace around the title, which you need to remove.

var movieName = $(".shown .title").html().trim();

Also, you should probably use .text() rather than .html(), in case there are embedded HTML tags.

var apiKey = "key";

var movieName = encodeURIComponent("deadpool");
var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + movieName;
console.log(url);

var movieName = $(".shown .title").html().trim();
console.log(movieName);

var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + encodeURIComponent(movieName);
console.log(url);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="shown">
<div class="title">
deadpool
</div>
</div>
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I tried it and it didn't work, and there are also no embedded items in the .title element. thanks anyway – drunkgummyboy Jan 02 '19 at 23:28
  • If you're using jQuery `$.get`, it's better to put the parameters in a data object rather than creating the query string by hand. It will encode everything properly. – Barmar Jan 02 '19 at 23:30
  • I've added a snippet showing that both of them create the same URL. – Barmar Jan 02 '19 at 23:35
  • `var movieName = $(".shown .title").text().trim(); console.log(movieName); var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + encodeURI(movieName); console.log(url);` it is working with this code, thanks a lot – drunkgummyboy Jan 02 '19 at 23:42