日期:2014-05-16  浏览次数:20677 次

AJAX 获取数据 乱码,原因何在??
Index.aspx 页面 


<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
  <title>无标题页 </title> 
<script language="javascript" type="text/javascript"> 
// 

var xmlhttp; 

function Button1_onclick() 

  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  xmlhttp.open("GET","http://localhost/WebApplication1/Index2.aspx",true); 
  xmlhttp.onreadystatechange = stateChange; 
  xmlhttp.send(null); 
   


function stateChange() 

  if(xmlhttp.readystate==4 && xmlhttp.status==200) 
  { 
  var data = xmlhttp.responseBody; 
  document.getElementById("divMy").innerHTML = data; 
  } 


// ]]> 
</script> 
</head> 
<body> 
  <form id="form1" runat="server"> 
  <div id="divMy"> 
   
  </div> 
  <div> 
  <input type="button" value="点击获取数据" id="Button1" onclick="return Button1_onclick()" /> </div> 
  </form> 
</body> 
</html> 


Index2.aspx 页面 

protected void Page_Load(object sender, EventArgs e) 

  Response.Write("你好于娜"); 
  Response.End(); 



---------------------- 


为什么AJAX获取的数据是乱码? 原因何在? 两张页面都是UTF-8编码格式输出的,为什么还有错???

------解决方案--------------------
在服务器端用escape函数编码
在响应时用unescape函数解码