第一:"Loading ..."换成"数据加载中 ..."
修改MagicAjaxModule.cs文件
加入了_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
第二:提交表單亂碼問題
修改AjaxCallObject.js
修改MagicAjaxModule.cs文件
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// Init private fields
_threadAbortExceptionThrown = false;
_request = context.Request;
_response = context.Response;
_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// Create a new context and add it to the items collection for later retrieval
// by MagicAjaxContext.Current
_magicAjaxContext = new MagicAjaxContext();
HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
// Check if the request is for the embedded AjaxCallObject.js script
if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
...
{
HttpContext context = ((HttpApplication)sender).Context;
// Init private fields
_threadAbortExceptionThrown = false;
_request = context.Request;
_response = context.Response;
_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
// Create a new context and add it to the items collection for later retrieval
// by MagicAjaxContext.Current
_magicAjaxContext = new MagicAjaxContext();
HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
// Check if the request is for the embedded AjaxCallObject.js script
if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
...
加入了_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
第二:提交表單亂碼問題
修改AjaxCallObject.js
AjaxCallObject.prototype.DoAjaxCall =
function(eventTarget, eventArgument, ajaxCallType, ajaxScopeID, additionalData) {...... this.XmlHttp.open("POST", thePage, true); this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); }; this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); this.XmlHttp.send(theData); }
可以看出在这里提交了theData数据,我用alert(theData)的方法将数据在提交前显示出来,发现提交的数据都直接显示是中文,然后再跟踪程序到后台,发现就已经是乱码了,这说明这个 AjaxCallObject.js 本身并没有对这个post的数据进行编码处理。
然后,就好办了,使用jscript里的 escape()函数将待post的数据在提交前编码一下,就不会出现中文了。代码如下:
if ( __bPageIsStored && eName == '__VIEWSTATE' ) continue; var type = curElem.type; var val = curElem.value; //这里对待提交的中文进行编码 //wangzhen 2006-07-06 val = escape(val); if ( type == "submit" || type == "button" ) continue; val = this.EncodePostData(val); if ( type == "select-multiple" || type == "select-one" ) { var selectLength = curElem.options.length; var optNameStr = this.EncodePostData(eName); for (var j=0; j < selectLength; j++) if (curElem.options[j].selected) theData = theData + optNameStr + '='
+ escape(curElem.options[j].value) + '&';//当页面里有dropdownlist时,还有这里的编码也要处理 } else if ( (type != "checkbox" && type != "radio") || curElem.checked ) { theData = theData + this.EncodePostData(eName) + '=' + val + '&'; }
除此之外,还要找到如下这段代码并按注释修改:
AjaxCallObject.prototype.EncodePostData = function(data) { //由于已经使用了escape方法对postback的值进行了编码,这里不再替换“%” //wangzhen 2006-07-06 //return data.split("%").join("%25").split("=").join("%3d").
//split("&").join("%26").split("+").join("%2b"); return data.split("=").join("%3d").split("&").join("%26").split("+").join("%2b"); }
这样就解决了所有的中文乱码问题!