我的目的是使用combobox加载ASHX文件的JSON,但不知道为什么总是加载不到数据。前台代码: <script type="text/javascript" language="javascript" >
$(function () {
$("#ddlGys").combobox({
url: '/Common/Helper.ashx',
mode:'remote',
valueFild: 'gys',
textFild:'gysbh'
});
});
</script>
<select id="ddlGys" class="easyui-combobox" />
后台:
public List<Supplier> GetSupplier()
{
DataTable dt = GingKo.Code.GetDT("select top 20 gysbh,gys from wz_gys");
List<Supplier> suppliers = new List<Supplier>();
for (int i = 0; i < dt.Rows.Count; i++)
{
Supplier tmp = new Supplier();
tmp.gys = dt.Rows[i]["gys"].ToString();
tmp.gysbh = Convert.ToInt32(dt.Rows[i]["gysbh"].ToString());
suppliers.Add(tmp);
}
return suppliers;
}
//该方法为combobox提供JSON
public string GetSuppliersJSON()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
Supplier s = new Supplier();
List<Supplier> suppliers = s.GetAllSupplier();
string json = serializer.Serialize(suppliers);
return json;
}
valueFild: 'gysbh', textFild:'gys'
var myOptions = { val1 : 'text1', val2 : 'text2' }; var mySelect = $('#mySelect'); $.each(myOptions, function(val, text) { mySelect.append( $('<option></option>').val(val).html(text) ); });
第二种方法:
$(document).ready(function() { $.ajax({ type: "POST", url: "Default.aspx/GetLanguageList", data: '', contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { $("#ddlLanguage").empty().append($("").val("[-]").html("Please select")); $.each(msg.d, function() { $("#ddlLanguage").append($("").val(this['Value']).html(this['Text'])); }); }, error: function() { alert("An error has occurred during processing your request."); } }); });
楼上的赞同。