Google 本书 jquery 自动完成来自 api url 呼叫 json 不工作
Google books jquery autocomplete from api url call json not working
我有一个搜索框 findBook,我正在尝试通过 json api 调用 google-books [=20] 使用 jquery 自动完成=] 显示书名、作者和缩略图。
当我在搜索框中输入时,没有任何反应。任何帮助,将不胜感激。自动完成的来源是 google-boosk api url。谢谢:)
<!DOCTYPE html>
<html>
<head>
<!-- Your web-app is https, so your scripts need to be too -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="/style.css" type="text/css">
<link rel="stylesheet" href="/home.css" type="text/css">
<script src='https://code.jquery.com/jquery-2.2.4.js'
integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00="
crossorigin="anonymous"></script>
<script src='https://code.jquery.com/ui/1.12.0/jquery-ui.js'></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" rel="stylesheet" media="screen" />
<script type='text/javascript'>
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<script type="text/javascript">
$(document).ready(function(){
$("#findBook").autocomplete({
source: function (request, response) {
$.ajax({
method:"GET",
dataType: "json",
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
success: function (data) {
console.log(data);
var transformed = $.map(data.items.volumeInfo, function (book) {
return {
title: book.title,
author: book.author,
image: book.imageLinks.thumbnail
};
});
response(transformed);
},
error: function () {
response([]);
}
});
}
});
});
</script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search.." id="findBook" />
</div>
</body>
</html>
您正在加载两个不同的 jQuery 版本 jquery-2.2.4
和 jquery/3.3.1/jquery.js
以及它在 jquery UI
之后加载的版本 3。这就是问题所在。
我只需删除 jquery-2x
并将 jquery3x
移到 jquery UI
之前。事实上,如果你只是在 jquery-UI
之前移动 jquery-3x
而不删除任何东西,那应该是可行的。
<!DOCTYPE html>
<html>
<head>
<!-- Your web-app is https, so your scripts need to be too -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="/style.css" type="text/css">
<link rel="stylesheet" href="/home.css" type="text/css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<script src='https://code.jquery.com/ui/1.12.0/jquery-ui.js'></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" rel="stylesheet" media="screen" />
<script type="text/javascript">
$(document).ready(function(){
$("#findBook").autocomplete({
source: function (request, response) {
$.ajax({
method:"GET",
dataType: "json",
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
success: function (data) {
console.log(data);
var transformed = $.map(data.items.volumeInfo, function (book) {
return {
title: book.title,
author: book.author,
image: book.imageLinks.thumbnail
};
});
response(transformed);
},
error: function () {
response([]);
}
});
}
});
});
</script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search.." id="findBook" />
</div>
</body>
</html>
$.map()
jQuery 函数需要一个数组来迭代。在您的情况下:data.items.volumeInfo
是 undefined
.
但是,你可以使用Array#map
JavaScript函数来遍历data.items
,这样:
var transformed = data.items.map(function(book) {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors,
image: book.volumeInfo.imageLinks.thumbnail
};
});
在上面的代码中,book 对象有一个 volumeInfo
属性,所以你可以访问它的属性来构建一个新的对象,在本例中 transformed
变量。
现在,要显示书名、作者和缩略图,您必须使用 _renderItem
函数修改 jQuery 自动完成插件的渲染 HTML。
类似于这个函数:
.autocomplete("instance")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + "<img src=\"" + item.image + "\" style=\"height: 40%;\" /><br />" + (item.author && item.author.length ? item.author.map(function(x) {
return x;
}).join(" | ") : '') + " - " + item.title + "</a>")
.appendTo(ul);
};
得到这个结果:
见本例:
$(function() {
$("#findBook").autocomplete({
source: function(request, response) {
$.ajax({
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
dataType: "json",
data: {
term: request.term
},
success: function(data) {
var transformed = data.items.map(function(book) {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors,
image: book.volumeInfo.imageLinks.thumbnail
};
});
response(transformed);
}
});
}
}).autocomplete("instance")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + "<img src=\"" + item.image + "\" style=\"height: 40%;\" /><br />" + (item.author && item.author.length ? item.author.map(function(x) {
return x;
}).join(" | ") : '') + " - " + item.title + "</a>")
.appendTo(ul);
};
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search..." id="findBook">
</div>
</body>
</html>
切记避免混用不同版本的 jQuery 库。
更新:只显示图片。
选项默认呈现,如果它们有文本要显示。但是,我们可以在文本中添加一个隐藏的 span
,它只会显示图像。
类似于此示例:
$(function() {
$("#findBook").autocomplete({
source: function(request, response) {
$.ajax({
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
dataType: "json",
data: {
term: request.term
},
success: function(data) {
var transformed = data.items.map(function(book) {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors,
image: book.volumeInfo.imageLinks.thumbnail
};
});
response(transformed);
}
});
}
}).autocomplete("instance")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<img src=\"" + item.image + "\" style=\"height: 40%;\" /><span hidden>" + item.image + "</span>")
.appendTo(ul);
};
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search..." id="findBook">
</div>
</body>
</html>
希望对您有所帮助!
我有一个搜索框 findBook,我正在尝试通过 json api 调用 google-books [=20] 使用 jquery 自动完成=] 显示书名、作者和缩略图。
当我在搜索框中输入时,没有任何反应。任何帮助,将不胜感激。自动完成的来源是 google-boosk api url。谢谢:)
<!DOCTYPE html>
<html>
<head>
<!-- Your web-app is https, so your scripts need to be too -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="/style.css" type="text/css">
<link rel="stylesheet" href="/home.css" type="text/css">
<script src='https://code.jquery.com/jquery-2.2.4.js'
integrity="sha256-gvQgAFzTH6trSrAWoH1iPo9Xc96QxSZ3feW6kem+O00="
crossorigin="anonymous"></script>
<script src='https://code.jquery.com/ui/1.12.0/jquery-ui.js'></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" rel="stylesheet" media="screen" />
<script type='text/javascript'>
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<script type="text/javascript">
$(document).ready(function(){
$("#findBook").autocomplete({
source: function (request, response) {
$.ajax({
method:"GET",
dataType: "json",
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
success: function (data) {
console.log(data);
var transformed = $.map(data.items.volumeInfo, function (book) {
return {
title: book.title,
author: book.author,
image: book.imageLinks.thumbnail
};
});
response(transformed);
},
error: function () {
response([]);
}
});
}
});
});
</script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search.." id="findBook" />
</div>
</body>
</html>
您正在加载两个不同的 jQuery 版本 jquery-2.2.4
和 jquery/3.3.1/jquery.js
以及它在 jquery UI
之后加载的版本 3。这就是问题所在。
我只需删除 jquery-2x
并将 jquery3x
移到 jquery UI
之前。事实上,如果你只是在 jquery-UI
之前移动 jquery-3x
而不删除任何东西,那应该是可行的。
<!DOCTYPE html>
<html>
<head>
<!-- Your web-app is https, so your scripts need to be too -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<link rel="stylesheet" href="/style.css" type="text/css">
<link rel="stylesheet" href="/home.css" type="text/css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<script src='https://code.jquery.com/ui/1.12.0/jquery-ui.js'></script>
<link href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" rel="stylesheet" media="screen" />
<script type="text/javascript">
$(document).ready(function(){
$("#findBook").autocomplete({
source: function (request, response) {
$.ajax({
method:"GET",
dataType: "json",
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
success: function (data) {
console.log(data);
var transformed = $.map(data.items.volumeInfo, function (book) {
return {
title: book.title,
author: book.author,
image: book.imageLinks.thumbnail
};
});
response(transformed);
},
error: function () {
response([]);
}
});
}
});
});
</script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search.." id="findBook" />
</div>
</body>
</html>
$.map()
jQuery 函数需要一个数组来迭代。在您的情况下:data.items.volumeInfo
是 undefined
.
但是,你可以使用Array#map
JavaScript函数来遍历data.items
,这样:
var transformed = data.items.map(function(book) {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors,
image: book.volumeInfo.imageLinks.thumbnail
};
});
在上面的代码中,book 对象有一个 volumeInfo
属性,所以你可以访问它的属性来构建一个新的对象,在本例中 transformed
变量。
现在,要显示书名、作者和缩略图,您必须使用 _renderItem
函数修改 jQuery 自动完成插件的渲染 HTML。
类似于这个函数:
.autocomplete("instance")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + "<img src=\"" + item.image + "\" style=\"height: 40%;\" /><br />" + (item.author && item.author.length ? item.author.map(function(x) {
return x;
}).join(" | ") : '') + " - " + item.title + "</a>")
.appendTo(ul);
};
得到这个结果:
见本例:
$(function() {
$("#findBook").autocomplete({
source: function(request, response) {
$.ajax({
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
dataType: "json",
data: {
term: request.term
},
success: function(data) {
var transformed = data.items.map(function(book) {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors,
image: book.volumeInfo.imageLinks.thumbnail
};
});
response(transformed);
}
});
}
}).autocomplete("instance")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + "<img src=\"" + item.image + "\" style=\"height: 40%;\" /><br />" + (item.author && item.author.length ? item.author.map(function(x) {
return x;
}).join(" | ") : '') + " - " + item.title + "</a>")
.appendTo(ul);
};
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search..." id="findBook">
</div>
</body>
</html>
切记避免混用不同版本的 jQuery 库。
更新:只显示图片。
选项默认呈现,如果它们有文本要显示。但是,我们可以在文本中添加一个隐藏的 span
,它只会显示图像。
类似于此示例:
$(function() {
$("#findBook").autocomplete({
source: function(request, response) {
$.ajax({
url: "https://www.googleapis.com/books/v1/volumes?q=" + request.term,
dataType: "json",
data: {
term: request.term
},
success: function(data) {
var transformed = data.items.map(function(book) {
return {
title: book.volumeInfo.title,
author: book.volumeInfo.authors,
image: book.volumeInfo.imageLinks.thumbnail
};
});
response(transformed);
}
});
}
}).autocomplete("instance")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<img src=\"" + item.image + "\" style=\"height: 40%;\" /><span hidden>" + item.image + "</span>")
.appendTo(ul);
};
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="searchBook">
<input type="text" placeholder="Search..." id="findBook">
</div>
</body>
</html>
希望对您有所帮助!