一般处理程序接受乱码问题
我在web.config的<system.web>节点下已经配置了
<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN"/>
但是用ajax传参的时候,还是挂掉了,乱码。。。。
js调试的时候,参数无乱码,传到一般处理程序就乱码,但是在地址栏直接敲入中文参数是没有乱码的!
什么情况?
function StopButton() {
document.getElementById(arguments[0]).disabled = true;
document.getElementById(arguments[0]).value = "提交(" + arguments[1] + ")";
if (--arguments[1] > 0) {
window.setTimeout("StopButton('" + arguments[0] + "'," + arguments[1] + ")", 1000);
}
if (arguments[1] <= 0) {
document.getElementById(arguments[0]).value = '提交';
document.getElementById(arguments[0]).disabled = false;
}
}
function SendComment() {
var commentuser = $("input#CommentUser").val().trim();
var commenttext = $("textarea#CommentText").val().trim();
if (commentuser == "" && commenttext == "") {
$("div#warning").html("<font color='red'>内容为空!<br />昵称为空!</font>");
return false;
}
if (commentuser == "") {
$("div#warning").html("<font color='red'>昵称为空!</font>");
return false;
}
if (commenttext == "") {
$("div#warning").html("<font color='red'>内容为空!</font>");
return false;
}
$("div#warning").html("");
$.ajax({
url: "addcomment.ashx?commentuser=" + commentuser + "&commenttext=" + commenttext,
type: "get",
success: function (data) {
if (data == "y") {
$("div#tishi").css({
background: "yellow",
"MARGIN-RIGHT": "auto",
"MARGIN-LEFT": "auto",
position: "absolute",
"z-index": 1,
left: "550px",
top: "400px"
});
$("div#tishi").html("<b>添加成功!</b>");
$("div#tishi").show(600);
//window.setInterval(function () { $("div#tishi").fadeOut(1000) }, 2000)
ShowComment();
$("input#CommentUser").val("");
$("textarea#CommentText").val("");
StopButton('CommentSubmit', 10);
}
}
});
}
$(document).ready(function () {
ShowComment();
});
<input type='button' class="postsubmit" id="CommentSubmit" value='提交' onclick="SendComment(1)" />
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (context.Request.QueryString["commentuser"] == null || context.Request.QueryString["commenttext"] == null)
{
context.Response.Write("parameter error");
}
else
{
string commentuser = context.Request.QueryString["commentuser"].ToString();
string commenttext = context.Request.QueryString["commenttext"].ToString();
Comment comment = new Comment()
{
Parentid = 1,
Commentuser = commentuser,
Commenttext = commenttext,
Commentreply = "",
Commentip = context.Request.UserHostAddress,
Commentdate = DateTime.Now
};
int n = CommentManager.AddComment(comment);
if (n > 0)
{
context.Response.Write("y");
context.Response.End();
}
else
{
context.Response.Write("n");
context.Response.End();
}
}
}
[最优解释]
url: "addcomment.ashx?commentuser=" +escape( commentuser) + "&commenttext=" +escape( commenttext),
加上escape就行了不用其他处理。
给分吧lz
[其他解释]
gb2312不是乱码吧
[其他解释]
这种东西 中文的貌似会出现乱码的
你可以先编码在传过去的 呵呵在ashx里面获取解码不不会出现乱码了
[其他解释]
addcomment.ashx这个里面输出的时候,你看一下编码,是否为GB2312,要和前台显示一致才行的,我以前也遇到过这种问题。
[其他解释]
http://blog.csdn.net/5653325/article/details/7942129
ASP.NET给ASHX文件传递中文参数
[其他解释]
你页面上指定了当前页面的编码没?
这个和Request/Response的ContentEncoding是两码事,不是通过web.config可以设定的。
你在web.config中指定的只是说传输的流是什么编码,但是实际在客户端浏览器中呈现出来的时候是依赖解析你HTML中指定的meta信息中的编码显示的。
[其他解释]
为什么你不先把传的内容编码后传,接收后再解码。这也就不会存在不同浏览器的乱码问题了吧
[其他解释]
+1 而且utf-8最好
[其他解释]
难道不行?js的encodeURIComponent编码后,后台Uri.UnescapeDataString解码下,反之一样。好像是这两个函数是对应的,js跟c#中编码的函数很多,但对些特殊字符是否编码是不同的
[其他解释]
web.confing中统一编码就行了,
加入该节点
<globalization culture="zh-CN" requestEncoding="gb2312" responseEncoding="gb2312" />
[其他解释]
那就是你,还在其它地步对它进行编码了,比如分页控件,默认是其它编码,这样就冲突,因为,这样配置过节点后,基本没有错误
[其他解释]
<globalization culture="zh-CN" requestEncoding="gb2312" responseEncoding="gb2312" />
首先 不要用gb2312,用UTF-8,在一个不要用escape进行编码,最正规的编码方式是encodeURIComponent ,至于为什么去博客园看看吧,
[其他解释]
asp.net 默认的是utf8的
[其他解释]
gb2312 改成utf-8试试。应当就能好。
[其他解释]
顺便提一下,在IE、Chrome中是没有乱码的,但是在FireFox中有乱码啊 ,亲。
[其他解释]
ShowComment() 方法还没贴上
function ShowComment() {
$.ajax({
url: "Handler1.ashx?parentid=1&t=" + Math.random(),
type: "get",
success: function (data) {
$("div#comment").html(data);
}
});
}
[其他解释]
我就只有3款浏览器 IE、Chrome、FireFox
测试的浏览器中只有火狐出现乱码问题 。
[其他解释]
我看了一下,我做输出的话 就一个 y 或者n啊 亲。
[其他解释]
你的博文我看了,并不能解决问题。
[其他解释]
<meta http-equiv="content-type" content="text/html;charset=gb2312">
[其他解释]
难道用javascript编码??
[其他解释]
哦,好吧 其实我没用过javascript编码
[其他解释]
呵呵 在一开始 我就说明了,添加了这个节点 。
[其他解释]
好吧,我承认用你的方法 成功了,有效,你想要多少分?
[其他解释]
求科普