我正在嘗試使用提交表單中的搜索詞呼叫我的 API,但我會收到一個錯誤,指出無法讀取未定義的屬性,我不確定為什么在發送 ajax 請求時沒有資料,下面是我的代碼。
$(function (){
let searchform = $('#searchForm')
searchform.on("submit",function(event){
event.preventDefault();
newNameInput = $('#search_term')
let searchterm = newNameInput.val();
console.log(searchterm)
$('#showList').empty();
$.ajax({
method: 'GET',
url: 'http://api.tvmaze.com/search/shows?q=' searchterm,
success: function(movies){
$.each(movies, function(i,movie){
$('#showList').append('<li ><a href="' movie._links.self.href '">' movie.show.name '</a></li>')
$('#showList').show();
})
}
})
})
})
網址:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TV Shows</title>
</head>
<body>
<h1>TV Shows</h1>
<div id="show" hidden></div>
<ul id="showList" hidden></ul>
<form id="searchForm">
<input type="text" id="search_term">
<label for="text">Search</label>
<button>Submit</button>
</form>
<a id="homelink" href="/" hidden>Back to All Shows</a>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script src="/public/js/index.js"></script>
</body>
</html>
當我檢查 URL 時,它會正確回傳資料,但不會從 Ajax 回傳資料。
http://api.tvmaze.com/search/shows?q=' searchterm
uj5u.com熱心網友回復:
您的 api 的回應似乎有點不同,您似乎show在訪問href和名稱$.each回圈時缺少一個鍵,請嘗試更改
...
movie._links.self.href
....
到
...
movie.show._links.self.href
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/373269.html
標籤:javascript html 查询 阿贾克斯
