当前位置: 首页 > 图文教程 > 网络编程 > Javascript > prototype 中文参数乱码解决方案

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 prototype 中文参数乱码解决方案


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 485 ::
收藏到网摘: n/a

在使用portotype,调用ajax方法时出现乱码,下面是解决方法需要的朋友可以参考下。 在使用portotype,调用ajax方法时出现乱码,此时,获取页面中加入
可以试试;; Page.Response.Charset = "gb2312";
如果不奏效,有人提供另外解决方案:
复制代码 代码如下:

request: function(url) {
this.url = url;
this.method = this.options.method;
this.encoding = this.options.encoding;
var params = this.options.parameters;
if(this.encoding.toUpperCase()=="UTF-8"){
encode=encodeURIComponent;
}else{
encode=escape;
}

在调用时,加上encoding: 'gb2312'
复制代码 代码如下:

var myAjax = new Ajax.Updater(
"dvContent",
url,
{
method: 'get',
parameters: pars,
encoding: 'gb2312'
});

另外还有一种方案;无论web.config中是否是"gb2312",都可以奏效。
因为网站编码是gb2312。在使用prototype的ajax时遇到中文参数乱码的问题。
方法如下;
1.将参数用encodeURI()编码
复制代码 代码如下:

var par;
par="param1="+"中文值"+"¶m2="+"其他值";
var url="server.aspx";
var myAjx=new Ajax.Request(url,{method: get',parameters: encodeURI(par) ,onComplete: submit});

2.接受端代码
复制代码 代码如下:

NameValueCollection nvc= HttpUtility.ParseQueryString(Request.Url.Query, Encoding.UTF8);
string title = (nvc["param1"] == null) ? "" : nvc["param1"];//接受中文

给出一个完整的调用示例,
复制代码 代码如下:

/**//*
Ajax企业名称是否存在检查
*/
function CheckCompanyExist()
{
var objtxtName=arguments[0];
var objBtnSubmit=arguments[1];
var objBtnCheck=arguments[2];
var currC_ID=arguments[3];
//alert($(arguments[0]).value);
var objtxtValue=$(arguments[0]).value;
$(objBtnSubmit).disabled=true;
if($F(objtxtName).trim())
{
$(objtxtName).disabled=!$(objtxtName).disabled;
var url="/tempAjaxService/";
var pars="module=CompanyExist&C_ID="+currC_ID+"&name="+objtxtValue;//$F(objtxtName);//objtxtValue;//
var myAjax = new Ajax.Request(url, {method: 'get', parameters: encodeURI(pars),onComplete : function() {
var txt = (''+arguments[0].responseText).evalJSON();
if(txt.Results==0||txt.Results==currC_ID)
{
//alert(''+arguments[0].responseText);
$(objBtnSubmit).value="提交我的企业信息";
$(objBtnSubmit).disabled=false;
alert("企业验证通过,您可以继续下一步操作");
}
else
{
$(objBtnSubmit).value="请先确认您的企业信息是否已存在本网中";
if(window.confirm('此企业已存在,想获得该公司管理权限,点击“确定”,了解详情!!\r\n如果不能打开窗口,请检查并取消您所用浏览器的“弹出窗口拦截”功能。\r\n或在浏览器地栏中输入以下地址\r\nhttp://www.ruanchen.com/'))
window.open ('http://www.ruanchen.com/','newwindow','height=160,width=800,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
}
$(objtxtName).disabled=!$(objtxtName).disabled;
}});
}

Code
复制代码 代码如下:

//Page.Response.Charset = "gb2312";
NameValueCollection nvc = System.Web.HttpUtility.ParseQueryString(Request.Url.Query, Encoding.UTF8);
Name=SQLParser.StringParse(nvc["name"]);