[已解决问题] 百度的站内搜索代码的问题?
提问时间: 2008-05-11 11:22
悬赏分:10 浏览:98 次
最近搞了个网店,想加上百度的站内搜索代码,可是从百度上copy来的代码不能用,从那个输入框输入的汉字,转到百度的时候就变成了乱码的
如:输入“百度”就会变成了“鐧惧害
http://www.baidu.com/baidu?word=%E7%99%BE%E5%BA%A6&tn=bds&cl=3&ct=2097152&si=www.roseav.com&s=on
应该是编码的问题吧,但是不知道改哪里,百度啊?
谁知道告诉我一下啊,
演示地址www.roseav.com站底下方

<SCRIPT language=javascript >
function g(formname) {
var url = "http://www.baidu.com/baidu";
if (formname.s[1].checked) {
 formname.ct.value = "2097152";
}
else {
 formname.ct.value = "0";
}
formname.action = url;
return true;
}
</SCRIPT>
<form name="f1" onsubmit="return g(this)">
<table bgcolor="#FFFFFF" style="font-size:9pt;">
<tr height="60"><td valign="top"><img src="http://img.baidu.com/img/logo-137px.gif" border="0" alt="baidu"></td>
<td>
<input name=word size="30" maxlength="100" >
<input type="submit" value="百度搜索"><br>
<input name=tn type=hidden value="bds">
<input name=cl type=hidden value="3">
<input name=ct type=hidden>
<input name=si type=hidden value="www.roseav.com">
<input name=s type=radio> 互联网
<input name=s type=radio checked> www.roseav.com
</td></tr></table>
</form>

提问者:椰子晚 - 初学一级
最佳答案
不知道是否影响你的其他页面代码,但就百度搜索的问题解决有两种方法,如下所述:
1、在你粘贴百度代码的页面中,如<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" ResponseEncoding="gb2312" %>增加【ResponseEncoding="gb2312"】属性值,这样你的ASPX页面就正确了,当然这个方法只是影响本页,而不会影响整个工程的页面,如最终测试页代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" ResponseEncoding="gb2312" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>百度搜索测试页</title>    
<script language="javascript" type="text/javascript">
function g(formname) {
var url = "http://www.baidu.com/baidu";
if (formname.s[1].checked) {
formname.ct.value = "2097152";
}
else {
formname.ct.value = "0";
}
formname.action = url;
return true;
}
</script>
</head>
<body>
<form name="f1" onsubmit="return g(this)">
<table bgcolor="#FFFFFF" style="font-size:9pt;">
<tr height="60"><td valign="top"><img src="http://img.baidu.com/img/logo-137px.gif" border="0" alt="baidu"></td>
<td>
<input name=word size="30" maxlength="100">
<input type="submit" value="百度搜索"><br>
<input name=tn type=hidden value="bds">
<input name=cl type=hidden value="3">
<input name=ct type=hidden>
<input name=si type=hidden value="www.guoxue.com">
<input name=s type=radio> 互联网
<input name=s type=radio checked> www.guoxue.com
</td></tr></table>
</form>
</body>
</html>
注意代码中的第一行变化。

2、也可以在Web.config中的<system.web></system.web>节中增加<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>。测试的Web.config代码如下:
<?xml version="1.0"?>
<!-- 
    注意: 除了手动编辑此文件以外,您还可以使用 
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在 
    machine.config.comments 中,该文件通常位于 
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<!-- 
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会 
            影响性能,因此只在开发过程中将此值 
            设置为 true。
        -->
<compilation debug="true">
<assemblies>
<add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
<!--
            通过 <authentication> 节可以配置 ASP.NET 使用的 
            安全身份验证模式,
            以标识传入的用户。 
        -->
<authentication mode="Windows"/>
<!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    
    <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
</system.web>
</configuration>
注意增加的倒数第三行<globalization />节。这样更改会影响整个工程的Encoding,应此请慎用此方法。

本人建议采用第一种方法,希望能解决你的问题。祝你好运!
2008/5/11 16:40:47 回答者:刚刚


提问者对于答案的评价:谢谢您了,这么认真热情的回答,同样谢谢楼下的那位兄弟。 我还是不用百度的搜索代码了,这么大的公司,连这样的问题都出,没办法。
其它回答(1)
楼上的兄弟辛苦了,只是我发现楼主的网站貌似是php开发的ecshop,楼主出现的我问题是由于www.roseav.com首页采用了utf-8编码,而百度则使用的是gb2312编码,所以提交表单的时候需要进行编码转换,楼上兄弟给你的asp.net(c#)的解决办法,

如果是php,你可以使用下面的类在表单提交前进行编码转换,呵呵

http://blog.sithere.net/article.asp?id=1677.html
5天前   回答者:SitHere - 菜鸟二级
评论
5天前   刚刚 :
呵呵,没注意,楼主的网站不是.net的,而是PHP的!
5天前   刚刚 :
PHP编码转换还真麻烦哈!(不太懂PHP,专注VS.Net)
4天前   玫瑰av :
二楼的要正确些,但是还是给分一楼了,一楼更辛苦。。。
4天前   刚刚 :
得了辛苦分,谢谢楼主了,不过我同样感谢二楼的正确答案!
如果有人是用.Net开发则可以采用我的回答,如果是PHP的则采用博友SitHere的回答。
1天前   SitHere :
呵呵,刚刚很热心
   您需要登录以后才能回答!
我的问题    我要提问


快到期问题

> 问题排行榜

有不合适内容,建议去除