我正在尝试在开发应用程序中实现 OMDB API,在控制台中我继续收到 401 Unauthorized 错误。我有一个apikey
来自 OMDB API 网站的活动,但我必须说它的用法让我感到困惑。
我在电子邮件中收到的内容如下所示:
http://www.omdbapi.com/?i=tt12345678&apikey=1234cae2
我试图以各种方式实现它,特别是这样:
/** catch a submission of the form
* and then take the value and call a function getMovies to reach out
* to API
*
**/
$(document).ready(() => {
$('#searchForm').on('submit', (e) => {
// put this in a variable
// console.log($('#searchText').val());
let searchText = $('#searchText').val();
getMovies(searchText);
// stops form from submitting
e.preventDefault();
});
});
function getMovies(searchText) {
// console.log(searchText);
axios.get('http://www.omdbapi.com/?s='+searchText).then((response) => {
console.log(response);
}).catch((err) => {
console.log(err);
});
}
但我不清楚我是否需要在那里附加 api,如果需要,哪一组数字是 api 密钥?我已经尝试了12345678&
和1234cae2
.
毕竟,我不确定问题是否是我通过内容交付网络引入的 axios 库,如下所示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Movie Info</title>
<link rel="stylesheet" href="https://bootswatch.com/4/cyborg/bootstrap.min.css">
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a href="index.html" class="navbar-brand">MovieInfo</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h3 class="text-center">Search for Any Movie</h3>
<form id="searchForm">
<input type="text" class="form-control" id="searchText" placeholder="Search Movie...">
</form>
</div>
</div>
<div class="container">
<div id="movies" class="row"></div>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="js/main.js"></script>
</body>
</html>
任何指导将不胜感激。
要搜索 omdb api,您必须提供 apiKey 并在 ajax 请求中传递相同的信息
示例 api http://www.omdbapi.com/?apikey=1234cae2&plot=short&t=Oceans
示例 ajax axios.get(' http://www.omdbapi.com/?apiKey=1234cae2 '+ '&t='+ searchText).then((response) => { console.log(response); })
要传递的参数列表在参数部分http://www.omdbapi.com/
注意:我将 url 与参数分开,因此您可以使用任何您喜欢的参数 id、title 或 search sting。此外,jQuery 还带有一个 ajax api,它也支持 promise。所以 axios 在这里可能是一个额外的负担。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句