尝试使用 WebMethods 用 sql 结果填充选择。
我可以获取和传递数据,但我注意到格式是 [["",""],["",""]] 而不是 [{"",""},{"","" }]。
当我使用 $.each 时,我得到单个字符作为选择的选项,输出中的每个字符,包括 JSON 分隔符。
VB
Dim MArray()() As String = New String(sqlDataset.Tables(0).Rows.Count)() {}
Dim i As Integer = 0
For Each rs As DataRow In sqlDataset.Tables(0).Rows
MArray(i) = New String() {rs("suppliername").ToString(), rs("supplierid").ToString()}
i = i + 1
Next
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Dim sJSON As String = js.Serialize(MArray)
Return sJSON
jQuery
$(document).ready(function () {
$.ajax({
type: "POST",
url: "SupplierAdmin.aspx/PopulateSupplierSelectDropDownList",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#supplierSelect").empty();
$.each(msg.d, function(name, value) {
$('#supplierSelect').append($('<option>').text(name).attr('value', value));
});
}
});
});