为什么我不能将var放在encodeURI中?

时间:2019-01-02 23:18:56

标签: jquery var encodeuricomponent

这为什么起作用?

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

1 个答案:

答案 0 :(得分:0)

HTML的标题周围可能有空白,您需要将其删除。

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

此外,如果有嵌入的HTML标签,您可能应该使用.text()而不是.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>