• JS调用ashx文件传递中文参数取不到值的解决方案


    实现在text中输入数据,areatext里动态搜索

    View Code
            <th class="t_r">
                                                请选择公司:
                                            </th>
                                            <td width="89%" class="kuang1">
                                                <asp:TextBox ID="txtKeyWord" runat="server"></asp:TextBox>
                                                <span class="red">*</span><br />
                                                <select id="txtCompanyList" multiple="multiple" name="D1" onclick="CompanySelect()"
                                                    style=" 380px; height: 119px">
                                                    <option></option>
                                                </select>
                                            </td>

    JS:

    View Code
    function BindCompanyList() {
                var comapyname = $("#txtKeyWord").val();
                $("#txtCompanyList").html("");
                $.getJSON("GetCompanyList.ashx?companyname=" + escape(comapyname), nullfunction(json) {
                    if (json != null) {
                        $.each(json, function(i) { $("#txtCompanyList").append($("<option></option>").val(json[i].id).html(json[i].companyname)) });
                    }
                });
            }
            function CompanySelect() {
                var Obj = document.getElementById("txtCompanyList");
                if (document.getElementById("txtCompanyList").options.length != 0) {
                    document.getElementById("txtKeyWord").value = Obj.options[Obj.selectedIndex].text;
                }
            }

    ASHX:

    View Code
            public void ProcessRequest(HttpContext context)
            {
                StringBuilder sb=new StringBuilder();
                if (context.Request.Params["companyname"] != null)
                {
                    DataTable dt = null;
                    string strcompanyname = context.Server.HtmlDecode(context.Request.Params["companyname"].ToString());
                    if (strcompanyname!="")
                    {
                        dt = new BLL.CZL_CompanyInfo().GetList(" companyname like '%" + strcompanyname + "%'").Tables[0];
                    }
                    else
                    {
                        dt = new BLL.CZL_CompanyInfo().GetAllList().Tables[0];
                    }
                    if (dt == null || dt.Rows.Count == 0)
                    {
                        return;
                    }
                    else
                    {
                        sb.Append("[");
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            //返回JOSN数据
                            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\",\"companyname\":\"" +Common.ProductAbout.ReturnStr(dt.Rows[i]["companyname"].ToString()) + "\"},");
                        }
                        sb.Remove(sb.Length - 1, 1);
                        sb.Append("]");
                    }
                }
                context.Response.ContentType = "application/json";
                context.Response.ContentEncoding = Encoding.UTF8;
                context.Response.Write(sb.ToString());
            }

    注意传递值的时候,js里用escape()对参数进行编码

    取得的时候,.cs中用context.Server.HtmlDecode()进行对参数的解密

  • 相关阅读:
    python scapy的用法之ARP主机扫描和ARP欺骗
    Python字典取键、值对
    python字典添加元素和删除元素
    Python删除列表元素
    获取本机的IP地址和mac地址
    Python查找电话号码归属地、邮编、运营商信息等
    Python的字符串格式化,%与format
    Python基础笔记一之字符转化、复数、位运算、除法运算、floor和ceil取整,round函数四舍五入
    FAILED: SemanticException Unable to determine if hdfs://tmaster:8020/user/root/words.db/test_t2 is encrypted
    Pyspark读取csv文件
  • 原文地址:https://www.cnblogs.com/yinpeng186/p/2196726.html
Copyright © 2020-2023  润新知