1. 程式人生 > >Json返回查詢的資料,前臺ajax獲取

Json返回查詢的資料,前臺ajax獲取

後臺程式碼:

[WebMethod]
    public string GetCusCode()
    {
        DataTable dt = C_c.SelectT("select CusCode from Customer");
        string jsonResult = null;
        if (dt.Rows.Count > 0)
        {
            jsonResult = JsonConvert.SerializeObject(dt,Formatting.Indented);
        }
        return jsonResult;
    }

前臺程式碼:

<pre name="code" class="html">$(document).ready(function () {
 
            var rowOption = "";
            $.ajax({
                type: "post",
                contentType: "application/json",
                url: "/webservice/WebService.asmx/GetCode",
                data: "{}",
                dataType: "json",
                success: function (data) {
                    var dt = eval("("+data+")");
                    alert(dt[0].Code);
                    for (var i = 0; i < dt.length; i++) {
                        rowOption += "<option value=" + dt[i].Code + ">" + dt[i].Code + "</option>";
                    }
 
                    $("#cusselect ").empty();
                    $("#cusselect ").append(rowOption);
                }
            });
});