Oren A
Oren A

Reputation: 5880

How to parse the returned object from "success" callback method in JQuery using AJAX

I have this stadard code for receiving a class from a web service:


function getStuff() {
  var callParams = "{'param1':'" + "oren"  + "'}"
  $.ajax({
  type: "POST",
  url: "http://192.168.5.223:8989/TolunaAPI.asmx/GetDummyType",
  data: callParams,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function (data) {
    alert(data);
  ....

This give an alert that says: [object Object].
I tried reading some related posts here and on JQuery documentation, but I can't understand what exactly is the "data" object.
What the web service returns is and object named DummyType:


public class DummyType
    {
       public ErrorTypes error;
       public NestedDummyType nested;
       public DummyType() { }
       public DummyType(string paramName)
       {
           error = ErrorTypes.None;
           nested = new NestedDummyType();
       }
    }
}

Which as you see has another object in it, namely NestedDummyType:

public class NestedDummyType
    {
       public string nestedString;
       public int nestedInt;
       public NestedDummyType()
        {
            nestedString = "Hello from nested";
            nestedInt = -1;
        }
    }

All of these are supposed to be returned in json format, but as mentioned, I can't seem to be able to interpret the received data.
How is this done?
Thanks.

Upvotes: 1

Views: 2770

Answers (4)

Jamiec
Jamiec

Reputation: 136114

Here's a useful little snippet I use in javascript when I want to find out about an object.

var s = "";
for(x in data)
  s += x + "=" + data[x] + "\r\n";
alert(s);

It will alert all methods/properties of the object data, but is not recursive (so you wouldnt see anything from nested.

Upvotes: 0

Pierre Henry
Pierre Henry

Reputation: 17487

If your server-side script effectively returns well-formed JSON, jquery will automatically parse it and the data object will contain the parsed object, i.e. your NestedDummyType object. Have you tried to do something like that in your success callback ?

success: function (data) {
    alert(data.nested.nestedString);
    alert(data.nested.nestedInt)
}

Upvotes: 1

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

So if you do this:

success: function (data) { 
    alert(data.nestedInt)'
    alert(data.NestedDummyType.nestedString);
...

do you get what you expect?

EDIT: Just in case you have asp.net, the 2.0 and 3.5 return different values in the data and this parse function in the dataFilter will resolve that issue and work with both versions:

   $.ajaxSetup({
        data: "{}",
        datatype: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataFilter: function(data)
        {
            var msg;
            if (typeof (JSON) !== 'undefined' &&
                typeof (JSON.parse) === 'function')
                msg = JSON.parse(data);
            else
                msg = eval('(' + data + ')');
            if (msg.hasOwnProperty('d'))
                return msg.d;
            else
                return msg;
        },
        error: function(xhr, textStatus, errorThrown)
        {
  //handle errors
        }
    });

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630429

You should be able to do this (the .d is because you're in .Net):

alert(data.d.nested.nestedString);

You're accessing the object just as you would in C#, just doing through the properties. The object d is the DummyType you're passing back, so just access the properties on it, the same as they're named in the C# type on the server, that's how they're serialized to the client.

If for some reason I've glossed over something and this doesn't work, remove the .d, but that shouldn't be the case.

Upvotes: 3

Related Questions