Ajax get方法 IE 下乱码
每个浏览器处理编码的格式不同。 ajax使用utf-8来编码发送数据,ie在发送时并没加上charset=utf-8,从而导致乱码(IE默认使用iso-8859-1编码)
JavaScript代码:
function CheckPerson() { //查询前判断;即根据 姓名 查询用户时,对于重名用户,则显示 用户名 输入框,以用户名查询 var userName = $("#SelPerson").val(); if ( userName == "请填写名称") { alert("请填写名称"); return false; } else { var dis = $("#trAccount").css("display"); if (dis == "none") { var username = encodeURI("name=" + userName); //注意,此处要进行编码,否则IE浏览器下会出现乱码问题 $.ajax("../ajax/SelectUsers.ashx", { type: "get", data: username, dataType: "json", cache: false, success: function (data) { if (data.State == "error1") { alert(data.Msg); $("#trAccount").show(); return false; } else if (data.State == "error2") { alert(data.Msg); return false; } else if (data.State == "ok") { return true; } else { alert("其他错误,请联系管理员"); return false; } } }) } else { if ($("#account").val().trim() == "") { alert("请填写账户"); return false; } else { return true; } } } }
asp页面前台代码:
<div class="div3"> <table border="0" cellspacing="0" cellpadding="0" style="width: 300px;"> <tr> <td colspan="2"> <h2>个人</h2> </td> </tr> <tr> <td>个人</td> <td><input runat="server" type="text" id="SelPerson" value="请填写名称" onfocus="if(this.value=='请填写名称'){this.value='';};" onblur="if(this.value==''){this.value=this.defaultValue;}" /> </td> </tr> <tr id="trAccount" style="display:none;"> <td>账户</td> <td><input runat="server" type="text" id="account" value="" /></td> </tr> <tr> <td><input class="hide" type="button" value="隐藏" /></td> <td><input id="Button3" type="submit" value="搜索" runat="server" onclick="return CheckPerson();" onserverclick="searchPersonal_Click" /></td> </tr> </table> </div>
后台页面代码:
protected void searchPersonal_Click(object sender, EventArgs e) { string where = ""; if (account.Visible==false)//账户输入框不显示,以用户名为条件 { where = " account ='" + account.Value + "'"; } else { where = " username='" + SelPerson.Value + "'"; } int num = tbUser.GetRecordCount(where); if (num > 0) { this.SetLimitPerson.Text = "person"; this.SetLimit.Text = ""; this.Rolename.Visible = false; this.Personal.InnerText = "操作个人-->" + this.SelPerson.Value; this.Personal.Visible = true; Bind_Repeater();//绑定模块功能列表 } }
一般处理页面:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; HttpRequest Request = context.Request; HttpResponse Response = context.Response; string userName = Request["name"]; tb_user user = new tb_user(); DataSet ds = user.GetIDByName(userName); if (ds.Tables.Count>0&&ds.Tables[0].Rows.Count>0) { int count = ds.Tables[0].Rows.Count; if (count > 1) { Response.Write("{\"State\":\"error1\",\"Msg\":\"用户名有重复,请填写具体账户\"}"); } else { Response.Write("{\"State\":\"ok\"}"); } } else { Response.Write("{\"State\":\"error2\",\"Msg\":\"没有此人,请重新填写\"}"); } }