主頁 > 知識庫 > 一句話解決AJAX中文亂碼問題[推薦]

一句話解決AJAX中文亂碼問題[推薦]

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 電子圍欄 阿里云 科大訊飛語音識別系統(tǒng) 團(tuán)購網(wǎng)站 服務(wù)器配置 銀行業(yè)務(wù) Linux服務(wù)器
下面是我的程序
HTML : 
復(fù)制代碼 代碼如下:


!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>無標(biāo)題頁/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 
復(fù)制代碼 代碼如下:

%@ 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 ="來自服務(wù)器的信息:您好 "+context.Request.QueryString["val"].ToString()+" --by time:"+DateTime.Now.ToLongTimeString();
context.Response.Write(tab);
}
public bool IsReusable {
get {
return false;
}
}
}

baidu搜了一大堆 大致意思是 AJAX提交數(shù)據(jù)時(shí),使用的是UTF-8的編碼 并且不可以設(shè)置為其他格式
如何解決呢 最后發(fā)現(xiàn)一個(gè)JS的函數(shù)escape與unescape 用escape()對將要提交的漢字進(jìn)行編碼,會(huì)出現(xiàn)大致%10%20的字符,類似與.NET中Server.UrlEncode()與Server.UrlDecode();
將JS獲得的表單值進(jìn)行重新編碼
Code
復(fù)制代碼 代碼如下:

var txtval=escape(document.getElementById("txt").value);
OK, 問題解決!
其他可能還有別的辦法至今沒遇到 希望這個(gè)辦法能幫到遇到這種困境的朋友
您可能感興趣的文章:
  • ajax+php中文亂碼解決辦法
  • Javascript和Ajax中文亂碼吐血版解決方案
  • JQuery AJAX提交中文亂碼的解決方案
  • jquery的ajax()函數(shù)傳值中文亂碼解決方法介紹
  • JQuery AJAX 中文亂碼問題解決
  • jquery.ajax的url中傳遞中文亂碼問題的解決方法
  • JQuery中Ajax的Post提交在IE下中文亂碼的解決方法
  • ajax獲取數(shù)據(jù)中文亂碼問題最簡單的完美解決方案

標(biāo)簽:江蘇 萍鄉(xiāng) 大理 棗莊 蚌埠 衢州 廣元 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《一句話解決AJAX中文亂碼問題[推薦]》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266