javascript 转URL传中文参数乱码问题解决办法

Javascript 部份:

  <script type="text/javascript">      

 function scriptUpdate(controlid) {
            try {
                var myxmlhttp = false;
                if (window.XMLHttpRequest) {
                    myxmlhttp = new XMLHttpRequest();
                } else {
                    myxmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                myxmlhttp.onreadystatechange = function() {
                    if (myxmlhttp.readyState < 4) {
                        //document.getElementById("myDiv").innerHTML = "数据读取中....";
                    }

                    if (myxmlhttp.readyState == 4 && myxmlhttp.status == 200) {
                        //document.getElementById("myDiv").innerHTML = myxmlhttp.responseText;
                    }
                }
                var str = controlid.id.replace("etxt", "").replace("eres", "");
                var txt_Description = document.getElementById("etxt" + str).value;
                var txt_Remarks = document.getElementById("eres" + str).value;              
                myxmlhttp.open("Get", "Default2.aspx?Remarks=" + encodeURIComponent(txt_Remarks) + "&Description=" + encodeURIComponent(txt_Description) + "&key=" + str + "&isNaM=" + Math.random(), true);
                myxmlhttp.send();
            } catch (e) {
                alert("Ajax错误!")
            }
        }   

    </script>

后台代码部份:

 protected void Page_Load(object sender, EventArgs e)
    {     

        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request["key"]))
            {
                SetTree(0);
            }
        }
        if (!string.IsNullOrEmpty(Request["Description"]) && !string.IsNullOrEmpty(Request["key"]))
        {
            var n = from x in DataContext.DirectoryTrees where x.ID == Convert.ToInt32(Request["key"]) select x;
            foreach (var j in n)
            {
                j.ModifyBy ="000";
                j.ModifyDate = System.DateTime.Now;
                j.Remarks =string.IsNullOrEmpty(Request["Remarks"])?"":Server.UrlDecode(Convert.ToString(Request["Remarks"]));
                j.Description = string.IsNullOrEmpty(Request["Description"]) ? "" : Server.UrlDecode(Convert.ToString(Request["Description"]));  
               
            }
            DataContext.SubmitChanges();
        }
    }

 

 

 

 

1/注意红色字部份,在Javascript中使用了encodeURIComponent()进行编码转换

2/其它后台不用使用Server.UrlDecode()解码也不会乱码了

你可能感兴趣的:(javascript 转URL传中文参数乱码问题解决办法)