首页域名资讯 正文

告诉你怎样解决Ajax中文乱码问题

2024-11-13 5 0条评论

js代码:

得到XmlHttpRequest的类

Code

function HttpRequest()
{
//取得Request对象
this.Request=function(){
try
{
if(window.XMLHttpRequest) request=new XMLHttpRequest();
if(!request)request=new ActiveXObject("Microsoft.XMLHTTP");
if(!request)request=new ActiveXObject("Msxml2.XMLHTTP");
return request;
}
catch(e)
{
alert("不支持XMLHTTPRequest");
}}
}

调用过程:

Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxTest._Default"  %>

<!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>Untitled Page</title>
<script language="javascript" type="text/javascript" src="User.Base.js"></script>
<script language="javascript" type="text/javascript">
function Check()
{
var request=new HttpRequest().Request();
request.onreadystatechange=function (){
if(request.readyState==4)
{
if(request.status==200)
{
alert(request.responseText);
}
else
{
alert(request.responseText);
}
}
}
var value=document.getElementById("val").value;
request.open("POST","Default.aspx?Value="+value,true);
//设置防止乱码的方法,只要一句话就行
request.setRequestHeader("Content-Type","text/html;charset=gb2312");
request.send(null);
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<input id="val" type="text" onblur="Check()" />
</div>
</form>
</body>
</html>


后台取得输入值:

protected void Page_Load(object sender, EventArgs e)
{
string value = Request["Value"];
}

测试过,可以得到正确的输入字符。




纵横数据面向全国提供域名注册、虚拟主机、云服务器、服务器托管与租用,如需了解,请联系QQ: 171356849 微信:zh18159893430 咨询,谢谢!

文章版权及转载声明

本文作者:亿网 网址:https://edns.com/ask/post/130154.html 发布于 2024-11-13
文章转载或复制请以超链接形式并注明出处。