0

I'm trying to parse JSON result retrieved from asp.net web method.

[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 received is somethink like below.

[{"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.");
    }
});

I'm receiving the error codes.length is undefined. I also tried codes.d.length but no luck

1
  • why are you using async false? Commented Nov 13, 2015 at 1:56

1 Answer 1

2

If you have specified the dataType to json no need to parse again

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);

}

should be

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

    }

}

demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.