views:

24

answers:

2

I am making a JSON call to web method which is defined in code behind. The web method returns a class object.The class returns 3 properties one of type list and 2 integers. I am accessing these in the following manner:

success: function(result) {
                    alert(result);
                    alert(result.LookCount);
                    alert(result.length);
                    if(result.LookCount  > 0)
                    {
                        var Info = "";

                        for(var i = 0;i < result.LookUps.length; i++)
                        {
                           Info += CreateLookUpGrid(result.LookUps[i].Client,result.LookUps[i].ClientOrg);
                        }

alert(result.LookCount) -> alerts undefined and when i alert result it shows me the compelte result string which has all data. So the data is returned correctly by web method. But I am unable to access it.

+1  A: 

You need to convert the result string into an object. If you're using the latest version of jQuery, you can use its parseJSON method:

var data= $.parseJSON(result);
if (data.LookCount > 0) {
    ...
}
Jeff Sternal
A: 

Likely, you need to use result.d instead of result. ASP.NET tries to implement some security by wrapping the JSON object in a "d" property so nothing gets accidentally evaluated and run on the client.

If you're doing something that actually returns a string (as your post indicates when describing the alert results), you'll need to parse the string into a JSON object. You can do this with JQuery, or another javascript file like JSON2.

John Fisher