为什么我不能在 encodeURI 中放入一个 var?
why cant i put a var in a encodeURI?
为什么会这样?
var movieName = encodeURI("deadpool");
var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + movieName;
这不是吗?;我已经放了一个控制台日志来检查并且有效
var movieName = $(".shown .title").html();
console.log(movieName);
var url = "https://api.themoviedb.org/3/search/movie?api_key=" + apiKey + "&query=" + encodeURI(movieName);
HTML 标题周围可能有空格,您需要将其删除。
var movieName = $(".shown .title").html().trim();
此外,您可能应该使用 .text()
而不是 .html()
,以防嵌入 HTML 标签。
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>