【问题标题】:Getting Google Site Search to work with my website让 Google Site Search 与我的网站一起使用
【发布时间】:2017-01-16 23:40:38
【问题描述】:

我有一个网站。如果您尝试使用搜索框,则会出现服务器错误。由于某种原因,它没有正确连接到 Google Site Search,并且不会将结果带到页面上。在我将它上传到服务器之前,它至少把我带到了搜索结果页面,但它只是给出了一个错误。我是自己设置此 Google Site Search 的新手,我不明白我缺少什么才能使其正常工作。

搜索.html:

<div id="cse" style="width: 100%;">Loading</div>

<script src="http://www.google.com/jsapi" type="text/javascript"> </script>
<script type="text/javascript">

  google.load('search', '1');
 google.setOnLoadCallback(function(){
  var customSearchControl = new       google.search.CustomSearchControl('xxxxxxxxxxxxxxxxxxx');
  customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
  customSearchControl.draw('cse');
$(".gsc-input").val("<?php echo $_POST['q']; ?>");//insert into  search field requested search text
$(".gsc-search-button").click();//call button click event, show   results
}, true);
</script>
<link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" />

从 index.html 或主页搜索框代码:

 <form class="navbar-form navbar-right" action="search.html" method="post">
    <div class="form-group">
      <input type="text" name="q" class="form-control" placeholder="Search...">
    </div>
    <button type="submit" name="search" value="Search" class="btn btn-default">Submit</button>
  </form>

搜索并提交后的现场错误代码:

服务器错误

405 - 不允许用于访问此页面的 HTTP 动词。 您正在查找的页面无法显示,因为尝试访问的方法(HTTP 动词)无效。

控制台错误:

加载资源失败:服务器响应状态为 405 (Method Not Allowed)

正确连接 method="get" 后出现新的控制台错误:

【问题讨论】:

  • 首先与我们分享您的代码。
  • 添加代码和错误。没有人可以从 URL 中看到服务器代码。我看到的错误看起来像是 IIS 错误。
  • 不要发出 POST 请求,尝试向 google 搜索 URL 发出 GET 请求。或与之相反。我的意思是 HTTP 动词是 GET、POST、PUT、OPTION 等。确保您使用的是允许的动词(目标允许)。哦,在这种情况下使用method="get"
  • 我会尝试 GET 选项,我假设我只是用它替换 POST 位?
  • 在将 method="post" 替换为 "get" 后,它现在将您带到正确的页面,但现在我收到了我在上述问题中更新的新错误消息。感谢您的帮助。

标签: php twitter-bootstrap search


【解决方案1】:

我只是通过将我的 search.html 更改为 search.php 然后将其重新加载到服务器来解决此问题。有道理,事情就是这么简单。代码如下,感谢所有帮助我的人。

<form class="navbar-form navbar-right" action="search.php" method="get">
    <div class="form-group">
      <input type="text" name="q" class="form-control" placeholder="Search...">
    </div>
    <button type="submit" name="search" value="Search" class="btn btn-default">Submit</button>
</form>

<script type="text/javascript">

 google.load('jquery', '1');
 google.load('search', '1');
 google.setOnLoadCallback(function(){
 var customSearchControl = new google.search.CustomSearchControl('your-ID-here');
 customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
customSearchControl.draw('cse');
$(".gsc-input").val("<?php echo $_GET['q']; ?>");//insert into search field requested search text
$(".gsc-search-button").click();//call button click event, show  results
}, true);

</script>

【讨论】:

    猜你喜欢
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2013-11-15
    • 2016-09-05
    • 2012-02-19
    相关资源
    最近更新 更多