通过更改cx和API密钥,我已成功将以下示例用于我自己的cse搜索引擎。我现在正在为我的用户创建一个表单,以使其能够更改查询。因此,我想将示例中的&q = cars更改为根据表单输入而更改的字符串。我的字符串名称是que,我尝试了&q =“ + que +”以及大约一千种其他方式都没有成功。我是否获得了单词que的搜索结果列表或完全没有结果?有任何想法吗?正确的语法应该如何?
<html>
<head>
<title>JSON Custom Search API Example</title>
</head>
<body>
<div id="content"></div>
<script>
function hndlr(response) {
for (var i = 0; i < response.items.length; i++) {
var item = response.items[i];
// in production code, item.htmlTitle should have the HTML entities escaped.
document.getElementById("content").innerHTML += "<br>" + item.htmlTitle;
}
}
</script>
<script src="https://www.googleapis.com/customsearch/v1?key=YOUR-KEY&cx=017576662512468239146:omuauf_lfve&q=cars&callback=hndlr">
</script>
</body>
</html>
在浏览器中运行的客户端代码中使用JSON API不是一个好主意,因为任何人都可以查看您的API密钥。
在这种情况下,您应该使用自定义搜索元素:https://developers.google.com/custom-search/docs/tutorial/implementingsearchbox