如何在JavaScript中分别调用.Ashx类中的两个方法?

时间:2018-07-16 15:15:14

标签: javascript html methods autocomplete ashx

我有一个无效的方法ProcessRequest

public void ProcessRequest(HttpContext context)
        {
            string term = context.Request["term"] ?? "";
            List<string> listAddress = new List<string>();

            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spGetAddressByindex", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter()
                {
                    ParameterName = "@term",
                    Value = term
                });
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    listAddress.Add(rdr["Address"].ToString());
                }

            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            context.Response.Write(js.Serialize(listAddress));

        }

此方法只能通过其类PickupHandler.ashx.cs在我的AddressDate.html Javascript中进行调用

 <script src="Scripts/jquery-3.3.1.js"></script>
     <link href="Scripts/jquery-ui.css" rel="stylesheet" />
    <script src="Scripts/jquery-ui.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#textbox').autocomplete({
                source: 'PickupHandler.ashx'

            });

        });
    </script>
</head>
<body>
    <input type="text" id="textbox"/>
</body>
</html>

现在,我的Pickuphandler.ashx类中有一个名为ProcessRequest2的新方法,该如何在相同的JavaScript源代码中调用它?甚至有可能还是我应该仅将其保留在一种方法中,但似乎那样会使它变得困难?

public void ProcessRequest2(HttpContext context)
        {
            string term = context.Request["term"] ?? "";
            List<string> listAddress = new List<string>();

            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("spGetAddressByindex", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter()
                {
                    ParameterName = "@term",
                    Value = term
                });
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    listAddress.Add(rdr["Address"].ToString());
                }

            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            context.Response.Write(js.Serialize(listAddress));

        }

0 个答案:

没有答案