问题描述
我在网站上进行了自定义搜索,我想使用自定义搜索热门查询"选项显示最受欢迎的搜索.首先,Google在此过程中提供的代码是错误的-我一直收到错误的请求. 这篇SO帖子帮助我重建了正确的URL,但是现在我收到语法错误.
I've got a custom search on a website and I'd like to display the most popular searches using the Custom Search Popular Queries option. First off, the code Google gives in the process is wrong - I kept getting bad requests. This SO post helped me rebuild the correct URL, but now I'm getting a syntax error.
运行测试时,我从返回的JSON信息中在控制台中找到Uncaught SyntaxError: Unexpected token :
.
When I run the test, I get, Uncaught SyntaxError: Unexpected token :
in the console from the JSON information that comes back.
这是我的HTML:
<div id="queries"></div>
<script src="https://cse.google.com/query_renderer.js"></script>
<script src="https://www.google.com/cse/api/USERID/cse/CSEID/queries/js?view=overall&callback=(new+PopularQueryRenderer(document.getElementById('queries'))).render"></script>
在title
之后是冒号抛出错误,但我不知道为什么会发生这种情况.
The colon throwing the error is after title
, but I can't figure out why that would be happening.
{
"title" : "MTBoS Search Engine",
"popularQueries" : [ ...rest of the results array... ]
}
最后,我尝试通过复制并粘贴调用网址并从格式化检查器调用JSON响应,它返回了一些HTML错误.查询渲染器脚本可能有问题吗?
Lastly, I tried calling the JSON response from a formatting checker by copying and pasting the call URL and it returned with a handful of HTML errors. Could it be an issue with the query renderer script?
感谢您的帮助.
推荐答案
如果您在Google上查看该示例(您应该可以在 Google自定义搜索/统计信息和日志的底部看到它页面),您应该看到Google的示例使用了编码的双引号,而不是单引号:
If you look at the example on Google (you should be able to see it at the bottom of your Google Custom Search/Statistics and Logs page), you should see that instead of single quotes, Google’s example uses encoded double-quotes:
<script src="https://www.google.com/cse/api/USERID/cse/CSEID/queries/js?view=overall&callback=(new+PopularQueryRenderer(document.getElementById(%22queries%22))).render"></script>
我不明白为什么单引号在那里不起作用,但是,我已经证明至少对我来说它们不起作用.如果我在其中加上单引号,则会收到您看到的错误:
I do not understand why single quotes do not work there, however, I have verified that for me at least they do not. If I put single quotes there, I get the error you saw:
如果将百分号编码的引号放在此处,它会成功创建受欢迎查询的列表.
If I put percent-encoded quotes there, it successfully creates the list of popular queries.
这似乎是引起单引号的原因,而不是未对它们进行编码以供在URL中使用.如果我将%22替换为%27(编码的单引号),则会导致相同的意外令牌"错误.
It appears to be the single quotes that are the issue, not that they aren’t encoded for use in URLs. If I replace %22 with %27 (encoded single quotes), the same "Unexpected token" error results.
如果您有理由避免使用百分比编码,则为在法律上使用单引号包围属性,因此您可以执行以下操作:
If there is a reason you wish to avoid the percent-encoding, it is legal to use single quotes to surround attributes, so you could do this:
<script src='https://www.google.com/cse/api/USERID/cse/CSEID/queries/js?view=overall&callback=(new+PopularQueryRenderer(document.getElementById("queries"))).render'></script>
根据HTML规范,双引号在单引号内是合法的,并且我已经验证了它也可以在我的搜索页上使用.
According to the HTML specs, double quotes are legal inside of single quotes, and I’ve verified that this also works on my search page.
这篇关于Google自定义搜索的热门查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!