
2017-2-10 15:31 |作者: admin |查看: 1173 |评论: 0 |来自: 蚂蚁部落

使用ajax时ajax参数乱码ajax参数乱码,不可避免地将使用url传递参数. 尽管通常会传递英文字符模式的参数,但也不可避免地传递中文参数,但是这时您会发现可能会出现乱码.

首先让我们直接看一下代码,掌握该解决方案很容易:

[HTML] 纯文本查看 复制代码<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta name="author" content="http://www.softwhy.com/" /> <title>蚂蚁部落</title> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("show").innerHTML = xmlhttp.responseText; } } xmlhttp.open("GET", "demo/ajax/net/demoPara.aspx?webName="+escape("蚂蚁部落")+"&age=3", true); xmlhttp.send(); } window.onload = function () { var obt = document.getElementById("bt"); obt.onclick = function () { loadXMLDoc(); } } </script> </head> <body> <div> <div id="show"></div> <input id="bt" type="button" value="查看效果"/> </div> </body> </html>
c#后台代码如下:
[C#] 纯文本查看 复制代码using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demoPara : System.Web.UI.Page { string webName; int age; protected void Page_Load(object sender, EventArgs e) { webName =Server.UrlDecode(Request.QueryString["webName"]); age = Convert.ToInt32(Request.QueryString["age"]); Response.Write("欢迎来到" + webName + ",本站已经成立" + age + "周年。"); } } }解决方案非常简单. 首先用前景代码对中文进行编码,然后在背景中接收到中文后对其进行解码.
当然,不同的语言在后台有不同的解码方法,这不是一成不变的事情.
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-165541-1.html
#吴亦凡#生日会必须约期待新歌