将变量传递给 Google 自定义搜索引擎
是否可以将搜索变量传递到我嵌入网站的 Google 自定义搜索引擎中?我可以让搜索引擎工作,但我无法通过 POST 传递一个术语(它来自网站其他页面上的搜索按钮)
我试图破解我在这里找到的代码: http://code.google.com/apis/ajax/playground/?exp=search# hello_world
这就是我到目前为止所拥有的...($q 是我传递给它的术语)
<script type="text/javascript">
google.load('search', '1', {language : 'en'});
function OnLoad()
{
var customSearchControl = new google.search.CustomSearchControl('***my key****');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
searchControl.execute("$q");
}
google.setOnLoadCallback(OnLoad);
</script>
谢谢
Is it possible to pass a search variable into the Google Custom Search Engine that I have embedded on my website? I can get the search engine to work, but I can't pass it a term via POST (it's coming from a search button on other pages of the website)
I tried to hack the code I found here: http://code.google.com/apis/ajax/playground/?exp=search#hello_world
And this is what I have so far... ($q is the term I am passing to it)
<script type="text/javascript">
google.load('search', '1', {language : 'en'});
function OnLoad()
{
var customSearchControl = new google.search.CustomSearchControl('***my key****');
customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
searchControl.execute("$q");
}
google.setOnLoadCallback(OnLoad);
</script>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉,我知道这是一个糟糕的答案,但除了引用错误的变量名称之外,您实际上已经得到了正确的答案。哦,另外,顺便说一句,我还希望您对 $q 进行某种清理,以防有人在您的表单中发布类似的内容:term");alert("啊哈!
应该是:
另外,谢谢你的问题 - 我一直在寻找如何自己做到这一点!
Sorry, I know it's a crappy answer, but you've actually got it right apart from referencing the wrong variable name. Oh, also, as an aside, I would also hope you're doing some kind of sanitisation on $q, in case someone posted something like this to your form: term"); alert("aha!
should be:
Also, thank you for the question - I was looking for how to do this myself!
这是为了帮助任何使用 PHP 的人尝试实现同样的目标。上面的示例使用...
读取传入的参数。在 PHP 站点上,您将使用...
如果您的参数不在帖子中,则可以使用 $_GET 或 $_REQUEST。
当然,您应该首先清理输入。像这样的东西相当弱,但它是一个开始......
This is to help anyone using PHP trying to accomplish this same goal. The example above uses...
to read the parameter being passes in. On a PHP site you would use...
You could use $_GET or $_REQUEST if your parameter is not in the post.
Of course you should should sanitize the input first. Something like this is pretty weak but it's a start...
如果有人正在寻找更直接/简单的解决方案。您所要做的就是将搜索关键字传递到名为 q 的 GET 参数(从您的自定义表单到 GCS 所在的页面),GCS 将自动使用该搜索短语。
来源:https://developers.google.com/custom-search/json -api/v1/using_rest
In case someone is looking for a bit more straight forward / simple solution. All you have to do is to pass search keywords into GET parameter named q (from your custom form into page where your GCS is), GCS will automatically use that search phrase.
Source: https://developers.google.com/custom-search/json-api/v1/using_rest