zoukankan      html  css  js  c++  java
  • 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=" 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":"没有此人,请重新填写"}");
                }
            }
    
  • 相关阅读:
    13.困难重重
    02.Django的第一个网页
    03.Django模板
    01.Web开发之简介
    14.效率利器之多线程和线程池
    Silverlight实用窍门系列:37.Silverlight和ASP.NET相互传参的两种常用方式(QueryString,Cookie)
    Web编码规范中文乱码解决方案
    H*ber*ate Lazy属性
    Bat如何判断txt文本中第8行是否只有"符号,有则删除整行
    #每日一练 获取字典值
  • 原文地址:https://www.cnblogs.com/SunXiaoLin/p/3394172.html
Copyright © 2011-2022 走看看