jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表

2020-05-29 07:21:10易采站长站整理

url: 'GetPara.ashx?type=save',
type: 'POST',
dataType: 'json',
data: parameter,
timeout: 3000,
cache: false,
beforeSend: LoadFunction, //加载执行方法
error: erryFunction, //错误执行方法
success: succFunction //成功执行方法
})
function LoadFunction() {
}
function erryFunction() {
}
function succFunction(tt) {
}
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl1" runat="server" onchange="GetPara(this)">
</asp:DropDownList>
<ul id="list"></ul>
<input type="button" value="保存数据" onclick="SavePara()" />
</div>
</form>
</body>
</html>

<%@ WebHandler Language="C#" Class="GetPara" %>
using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Web.Script.Serialization;
public class GetPara : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
string SortId = context.Request["sortid"];
string Type = context.Request["type"];
if (Type=="get")
{
if (!string.IsNullOrEmpty(SortId))
{
DataTable dt = MSCL.SqlHelper.GetDataTable("select * from PR_PRODUCTPARAS where sortid='" + SortId + "' ");
List<Paras> list = new List<Paras>();
for (int i = 0; i < dt.Rows.Count; i++)
{
Paras a = new Paras();
a.id = dt.Rows[i]["PARAID"].ToString();
a.name = dt.Rows[i]["PARANAME"].ToString();
list.Add(a);
}
context.Response.Write(new JavaScriptSerializer().Serialize(list));
}
}
else if (Type == "save")
{
//反序列化json
System.IO.Stream stream = context.Request.InputStream;
System.IO.StreamReader sr = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("UTF-8"));
string sJson = sr.ReadToEnd();
if (sJson.Contains("&"))
{
string[] sArr = sJson.Split('&');
for (int i = 0; i < sArr.Length; i++)
{
string[] sArr1 = sArr[i].Split('=');
object id = sArr1[0];
object value = sArr1[1];
}
}
}
else
{ }
}
public bool IsReusable {
get {
return false;
}
}
public struct Paras
{
public string id;
public string name;
}
}