views:

50

answers:

2

I have passed some JSON to my page via a webservice. I used JSON.NET to convert XML to JSON. The JSON output looks ok to me, but I am unable to access some of the items in the response. I am not sure why it is not working. I am using jQuery to read the response and make the webservice call. Even when I try to read the length of the array it says 'undefined'

function GetFeed(){

        document.getElementById("marq").innerHTML = '';

        $.ajax({
            type: "POST",
            url: "ticker.asmx/GetStockTicker",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {                  

            var obj = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;

            for (var i = 0; i < obj.length; i++) {
                $('#marq').html(obj[i].person);
            }               

            }
        });

    }

This is a copy and paste of my response as it appeared in firebug:

{"d":"{\"?xml\":{\"@version\":\"1.0\",\"@standalone\":\"no\"},\"root\":{\"person\":[{\"@id\":\"1\",\"name\":\"Alan\",\"url\":\"http://www.google.com\"},{\"@id\":\"2\",\"name\":\"Louis\",\"url\":\"http://www.yahoo.com\"}]}}"}
+1  A: 

You should be able to read response without calling the ternary operator... anyway if you're trying to iterate over the persons array you should target response.d.root.person object, and not it's parent:

for (var i = 0; i < response.d.root.person.length; i++) {
  $('#marq').html(d.root.person[i].name //.url, ...);
}  
mamoo
A: 

I know that Jquery's Ajax complete had issue that it's always get called twice when request complete. I'm not sure if that's the case for success.