[英]How do I get both the key and the value of a dictionary returned to ajax call
In my static web method I populate a dictionary with the Bankid and Bankname that I select from a database. 在我的静态Web方法中,我使用从数据库中选择的Bankid和Bankname填充字典。 The Id goes to the Key and the Value goes to the value part.
Id转到键,Value转到值部分。 SO there're approximately 20 key-value-pairs in my dictionary.
因此,我的词典中大约有20个键值对。 Then I return this dictionary to my ajax call:
然后,我将此字典返回给我的ajax调用:
$.ajax({
type: 'POST',
url: 'AJAX.aspx/Banks',
data: '',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data)
{
$.each(data.d, function ()
{
$("#testDiv").append(this.Key + " " +this.Value + "<br/>");
});
},
error: function (x, e)
{
alert("The call to the server side failed. " + x.responseText);
}
});
In the above case I get 20 rows of undefined undefined. 在上述情况下,我得到20行未定义的未定义。 But if I remove both .Key and .Value from this I just get the value, so bank names.
但是,如果我从中同时删除.Key和.Value,我只会得到值,因此是银行名称。 I need both the key and the value because I'm going to populate a select element with them-key for value and the value for inner html.
我需要键和值,因为我要用它们填充select元素-值键和内部html值。 In case you want to see my webmethod, here it is:
如果您想查看我的网络方法,这里是:
[WebMethod]
public static Dictionary<string, string> Banks()
{
Dictionary<string, string> dict = new Dictionary<string, string>();
DatabaseProvider provider = GetDatabaseProvider();
provider.AddOutParameter("V_CUR", OracleType.RefCursor);
DataTable dt=provider.SelectDataTable("AGAPUS.PAYMENT.SP_BANKS", CommandType.StoredProcedure);
foreach (DataRow row in dt.Rows)
{
dict.Add(row["PAYMENTTYPE"].ToString(), row["PAYMENTTYPEID"].ToString());
}
return dict;
}
Looks more like a question about jQuery to me. 在我看来,这更像是一个关于jQuery的问题。 That said, try changing your success function to:
也就是说,尝试将成功功能更改为:
success: function (data)
{
$.each(data.d, function ( key, value )
{
$("#testDiv").append(key + " " + value + "<br/>");
});
}
The key and value are passed as parameters to the function when evaluating a map. 评估地图时,键和值将作为参数传递给函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.