Jquery ajax调用结果未定义

时间:2015-11-13 01:54:26

标签: jquery asp.net json ajax asp.net-ajax

我试图解析从asp.net web方法检索到的JSON结果。

[WebMethod]
        public static string readCheckOutResult()
        {
            string uKey = DateTime.Now.ToString("yyyyMMddHHmmss");
            string customerNo = HttpContext.Current.Session["customerNo"].ToString();
            Core core = new Core();
            DataTable dt = core.checkoutCustomerCart(customerNo, uKey); //dt = checked out product

            string JSONResult = DataTableToJSON.DataTableToJsonObj(dt);

            return JSONResult;
        }

Json Result收到的是下面的想法。

[{"CustomerNo":"33157880","ProductNo":"ALDC125DC-DIXON","CustomerProductNo":"","ProductDescription":"32MM AL D/CAST CAM TYPE DC  DIX     ",
"UOM":"  ","Price":"11.93200","Qty":"1","SubTotal":"11.93200","uKey":"201511131242","ModifyDate":"13/11/2015 12:42:25 PM","ID":"190"}]


$.ajax({
    type: "POST",
    url: "checkout.aspx/readCheckOutResult",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: false,
    success: function(codes) {
        alert(codes.length);
        var data = jQuery.parseJSON(codes.d);
        for (var i = 0; i < data.d.length; i++) {
            alert(data.d[i].CustomerNo);

        }

        alert(concatstring);

    },
    error: function() {
        alert("Failed to Retrieve Data.");
    }
});

我收到错误代码。长度未定义。我也试过codes.d.length但没有运气

1 个答案:

答案 0 :(得分:2)

如果您已指定dataType to json无需再次解析

success: function(codes) {
    alert(codes.length);
    var data = jQuery.parseJSON(codes.d);
    for (var i = 0; i < data.d.length; i++) {
        alert(data.d[i].CustomerNo);

    }

    alert(concatstring);

}

应该是

success: function(codes) {
    alert(codes.length);
    for (var i = 0; i < codes.length; i++) {
        alert(codes[i].CustomerNo);

    }

}

demo