一句話解決AJAX中文亂碼問題[推薦]
更新時間:2008年10月10日 21:48:27 作者:
寫了個通過一般處理程序處理的AJAX小程序 結果遇到了國內程序員遇到的最多的問題:中文亂碼
下面是我的程序
HTML :
<!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>
<title>無標題頁</title>
<script type="text/javascript" language="javascript">
var xmlhttp;
function createXMLHttprequest()
{
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
}
function inData()
{
var txtval=document.getElementById("txt").value;
createXMLHttprequest();
xmlhttp.open("GET","request.ashx?val="+txtval,true);
xmlhttp.onreadystatechange=getData;
xmlhttp.send(null);
}
function getData()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
document.getElementById("showDT").innerHTML=xmlhttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" action="">
<div>請輸入姓名:
<input type="text" id="txt" />
<input type="button" value="提交" id="asdf" onclick="inData()" />
<span id="showDT" ></span>
</div>
</form>
</body>
</html>
request.ashx :
Code
<%@ WebHandler Language="C#" Class="request" %>
using System;
using System.Web;
public class request : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string tab ="來自服務器的信息:您好 "+context.Request.QueryString["val"].ToString()+" --by time:"+DateTime.Now.ToLongTimeString();
context.Response.Write(tab);
}
public bool IsReusable {
get {
return false;
}
}
}
baidu搜了一大堆 大致意思是 AJAX提交數(shù)據(jù)時,使用的是UTF-8的編碼 并且不可以設置為其他格式
如何解決呢 最后發(fā)現(xiàn)一個JS的函數(shù)escape與unescape 用escape()對將要提交的漢字進行編碼,會出現(xiàn)大致%10%20的字符,類似與.NET中Server.UrlEncode()與Server.UrlDecode();
將JS獲得的表單值進行重新編碼
Code
var txtval=escape(document.getElementById("txt").value);
OK, 問題解決!
其他可能還有別的辦法至今沒遇到 希望這個辦法能幫到遇到這種困境的朋友
HTML :
復制代碼 代碼如下:
<!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>
<title>無標題頁</title>
<script type="text/javascript" language="javascript">
var xmlhttp;
function createXMLHttprequest()
{
if(window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
}
function inData()
{
var txtval=document.getElementById("txt").value;
createXMLHttprequest();
xmlhttp.open("GET","request.ashx?val="+txtval,true);
xmlhttp.onreadystatechange=getData;
xmlhttp.send(null);
}
function getData()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
document.getElementById("showDT").innerHTML=xmlhttp.responseText;
}
}
}
</script>
</head>
<body>
<form id="form1" action="">
<div>請輸入姓名:
<input type="text" id="txt" />
<input type="button" value="提交" id="asdf" onclick="inData()" />
<span id="showDT" ></span>
</div>
</form>
</body>
</html>
request.ashx :
Code
復制代碼 代碼如下:
<%@ WebHandler Language="C#" Class="request" %>
using System;
using System.Web;
public class request : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string tab ="來自服務器的信息:您好 "+context.Request.QueryString["val"].ToString()+" --by time:"+DateTime.Now.ToLongTimeString();
context.Response.Write(tab);
}
public bool IsReusable {
get {
return false;
}
}
}
baidu搜了一大堆 大致意思是 AJAX提交數(shù)據(jù)時,使用的是UTF-8的編碼 并且不可以設置為其他格式
如何解決呢 最后發(fā)現(xiàn)一個JS的函數(shù)escape與unescape 用escape()對將要提交的漢字進行編碼,會出現(xiàn)大致%10%20的字符,類似與.NET中Server.UrlEncode()與Server.UrlDecode();
將JS獲得的表單值進行重新編碼
Code
復制代碼 代碼如下:
var txtval=escape(document.getElementById("txt").value);
其他可能還有別的辦法至今沒遇到 希望這個辦法能幫到遇到這種困境的朋友
相關文章
AjaxFileUpload結合Struts2實現(xiàn)多文件上傳(動態(tài)添加文件上傳框)
本文是腳本之家小編給大家分享的AjaxFileUpload結合Struts2實現(xiàn)多文件上傳功能,如果項目需求是不確定多少個文件,我們需要動態(tài)的添加文件上傳框,具體實現(xiàn)代碼大家參考下本文2017-09-09
php ajax網(wǎng)站瀏覽統(tǒng)計功能的簡單實現(xiàn)
這個功能應該是很多網(wǎng)站都需要的,這里僅僅實現(xiàn)了一個基于文件的簡易版本,數(shù)據(jù)庫的版本請自行參考實現(xiàn),我這里實現(xiàn)的功能很不完善,比如未過濾是否為同一訪客,是否為同一IP等等,這里僅僅是給大家提供一個參考.2008-09-09

