<globalization fileEncoding="utf-8" />
<p><a id="link2" href="#" target="_blank">escape</a></p>
<script type="text/javascript">
var str = "aa=1&bb=" + escape("fish li + is me.") + "&cc=" + escape("大明王朝1368");
$("#link2").attr("href", "/test_url_decode.ashx?method=escape&" + str);
</script>
[img]http://img.1sucai.cn/uploads/article/2018010709/20180107090128_1_69602.png[/img] [/b]
注意:"fish li + is me." 中间的加号没有了。 解决这个问题有个简单的方法,那就是使用JQuery的$.param()方法,修改后的代码如下:
<script type="text/javascript">
var myobject = { aa: 1, bb: "fish li + is me.", cc: "大明王朝1368" };
$("#link1").attr("href", "/test_url_decode.ashx?method=param&" + $.param(myobject));
</script>
<p><a id="btnTestParam" href="javascript:void(0);">Click me! 【点击我】</a></p>
<div id="divResult"></div>
<script type="text/javascript">
$(function() {
$("#btnTestParam").click(function() {
$.ajax({
url: "/TestParam.ashx", type: "GET", cache: false,
data: { id: 2,
name: "fish li + is me.",
tel: "~!@#$%^&*()_+-=<>?|",
"x?x!x&x": "aa=2&bb=3&cc=汉字。", // 特殊的键名,值内容也特殊。
encoding: "见鬼去吧。?& :)",
中文键名: "大明王朝1368"
},
success: function(responseText) {
$("#divResult").html(responseText);
}
});
});
});
</script>
[img]http://img.1sucai.cn/uploads/article/2018010709/20180107090129_2_17735.png[/img] [/b]
[b]JavaScript中正确的URL编码方式[/b] 看过前面的示例,您有没有想过:为什么escape不能解决的问题,JQuery就能解决呢? 对于这个问题,我想还是先来看看MSDN中关于escape的说明(截图): [b][img]http://img.1sucai.cn/uploads/article/2018010709/20180107090130_3_20725.png[/img] [/b]
MSDN说的很清楚,我也没有必要再做解释。 不过,我想有人可能会问:我用POST提交数据呢?那可是不经过URL的。 是的,POST数据时,参数没有放在URL中,但是,仍然采用URL编码。 POST数据也采用URL编码,是因为:表单可以采用GET方式提交,那么数据将通过URL提交给服务器。 所以提交的数据都要经过URL编码。 我们再来看一下$.ajax是如何处理数据的提交过程的:
ajax: function( origSettings ) {
var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings);
// ............... 去掉一些无关的代码
// convert data if not already a string
if (s.data && s.processData && typeof s.data !== "string") {
// 注意下面这个调用
s.data = jQuery.param( s.data, s.traditional );
}
// Serialize an array of form elements or a set of
// key/values into a query string
param: function( a, traditional ) {
var s = [];
// ............... 去掉一些无关的代码
// If an array was passed in, assume that it is an array of form elements.
if ( jQuery.isArray(a) || a.jquery ) {
// Serialize the form elements
jQuery.each( a, function() {
add( this.name, this.value );
});
} else {
// ............... 去掉一些非重点代码
}
// Return the resulting serialization
return s.join("&").replace(r20, "+");
function add( key, value ) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : value;
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
}
}
<globalization requestEncoding="gb2312" responseEncoding="gb2312" />
$.ajax({
// 注意下面这行代码,它为请求添加一个自定义请求头
beforeSend: function(xhr) { xhr.setRequestHeader("x-charset", "utf-8"); },
url: "/TestParam.ashx", type: "GET", cache: false,
data: { id: 2,
name: "fish li + is me.",
tel: "~!@#$%^&*()_+-=<>?|",
"x?x!x&x": "aa=2&bb=3&cc=汉字。", // 特殊的键名,值内容也特殊。
encoding: "见鬼去吧。?& :)",
中文键名: "大明王朝1368"
},
success: function(responseText) {
$("#divResult").html(responseText);
}
});
beforeSend: function(xhr) { xhr.setRequestHeader("x-charset", "utf-8"); },
public class ContentEncodingModule : IHttpModule
{
public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(app_BeginRequest);
}
void app_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
HttpWorkerRequest request = (((IServiceProvider)app.Context)
.GetService(typeof(HttpWorkerRequest)) as HttpWorkerRequest);
// 注意:我并没有使用 app.Request.Headers["x-charset"]
// 因为:绝大部分程序不访问它,它将一直保持是 null,
// 如果我此时该问这个集合,会导致填充它。
// 我认为填充Headers集合比我下面的调用的成本要高很多,
// 所以,直接通过HttpWorkerRequest读取请求头对性能的损耗会最小。
string charset = request.GetUnknownRequestHeader("x-charset");
if( string.Compare(charset, "utf-8", StringComparison.OrdinalIgnoreCase) == 0 )
// ASP.NET在填充QueryString,Form时,会访问Request.ContentEncoding做为解码时使用的字符编码
app.Request.ContentEncoding = System.Text.Encoding.UTF8;
}
$(function() {
var cookie = $.cookie("TestJsRead");
$("#cookieValue").text(cookie);
});
cookie = new HttpCookie("TestJsRead", HttpUtility.UrlEncode("大明王朝1368"));
Response.Cookies.Add(cookie);
public void ProcessRequest(HttpContext context)
{
byte[] fileContent = GetFileContent();
context.Response.ContentType = "application/octet-stream";
string downloadName = "ClownFish性能测试结果.xlsx";
string headerValue = string.Format("attachment; filename="{0}"", downloadName);
context.Response.AddHeader("Content-Disposition", headerValue);
context.Response.OutputStream.Write(fileContent, 0, fileContent.Length);
}
[img]http://img.1sucai.cn/uploads/article/2018010709/20180107090131_4_53814.png[/img] [/b]
遗憾的是,在我的IE8中是这样的: [b][img]http://img.1sucai.cn/uploads/article/2018010709/20180107090131_5_34124.png[/img] [/b]
对于这个乱码问题,我们需要把代码做一点修改:string downloadName = "ClownFish性能测试结果.xlsx"; if( context.Request.Browser.Browser == "IE" ) downloadName = HttpUtility.UrlPathEncode(downloadName);
[img]http://img.1sucai.cn/uploads/article/2018010709/20180107090131_6_3395.png[/img] [/b]
[b]多语言数据的乱码问题[/b] 现在还有一种乱码问题是:同一个程序供多种不同字符集(语言)的用户在使用。 例如:程序是简体中文的,此时,繁体中文的用户无法保存繁体汉字(就算简体汉字能正常显示)。 当发现这种现象时,需要检查一下数据库的字段类型,是否是Unicode或者UTF-8, 因为当数据字段的字符集不支持多种语言时,乱码问题必定产生。 我建议在使用SQL SERVER时,保存文字的字段都使用N开头的类型, 如:nvarchar, nchar,除非明确知道要保存邮政编码或者md5值,才有必要使用char(xxx)这种数据类型。 类似的,在MySQL中,我建议使用UTF-8 [b] 乱码问题的总结[/b] ASP.NET的乱码问题一般与二个因素有关: 1. 选择了不恰当的字符编码,如:gb2312 2. 选择了不正确的URL编码方法,如:escape() 因此,解决方案其实也不难: 1. 字符编码选择 utf-8 ,包含文件编码,请求/响应编码,数据库字段类型。 2. URL编码方法选择encodeURIComponent,再次强烈推荐直接使用JQuery 我一直认为:正确的方法可以让我在无形中避开许多问题。 如果你还为乱码问题而烦恼,我建议你先想想你是否选择了不正确的编码(方法)。 点击此处下载示例代码:[url=http://xiazai.1sucai.cn/201612/yuanma/AspnetEncode_jb51.7z]demo [/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有