简体   繁体   中英

why cant i put a var in a encodeURI?

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);

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM