我从我的C#WebMethod获取数据。我不知道如何从我的WebMethod键和值绑定到我的下拉:使用jQuery绑定Ajax数据
C#
[WebMethod]
public static Dictionary LoadRestByCityState(string city, string state)
{
DataSet ds = new DataSet();
Database db = DatabaseFactory.CreateDatabase(ConfigManager.AppSettings[“ConnectionString.Data”]);
DbCommand dbCommand = db.GetStoredProcCommand(“sel_RestByCityState_p”);
db.AddInParameter(dbCommand, “@pListCity”, DbType.String, city);
db.AddInParameter(dbCommand, “@pListState”, DbType.String, state);
ds = db.ExecuteDataSet(dbCommand);
Dictionary rest = new Dictionary();
foreach (DataRow row in ds.Tables[0].Rows)
{
rest.Add(row[0].ToString(), row[1].ToString());
}
return rest;
}
jQuery的
function LoadRest() {
__state = $(“#State :selected”).val();
__state = ‘”‘ + __state + ‘”‘
__city = $(“#City :selected”).val();
__city = ‘”‘ + __city + ‘”‘
$.ajax({
type: “POST”,
url: “Default.aspx/LoadRestByCityState”,
data: ‘{“city”:’ + __city + ‘,”state”:’+ __state +’}’,
contentType: “application/json; charset=utf-8”,
dataType: “json”,
async: true,
cache: false,
success:
function (data) {
$.each(data[0], function(key….not sure about this stuff
alert(‘parks loaded’);
},
fail: function() {
alert(“Error.”);
}
});
return false;
}
数据 这正在传递给WebMethod
{
“state”: “AL”,
“city”: “Auburn”
}
当调试我的WebMethod时,我将鼠标悬停在“休息”上,它显示“密钥”和“值”。