1

I know this question might appear be too localized but I can't seem to get this to work.

Here is the JSON my method returns:

{ "dateTime" : "5/26/2011",
  "requestTime" : 0.1020102,
  "users" : [ { "Email" : "[email protected]",
        "Location" : "home",
        "Name" : "Joe M",
        "UserId" : "42a7eae4-d4fe-49a3-93df-cd0cf219ac95"
      },
      { "Email" : "[email protected]",
        "Location" : "Work",
        "Name" : "Test Name",
        "UserId" : "97a444fb-6e3d-482c-a966-dbd3e0c739c8"
      }
    ]
}

The jQuery I'm using is like so:

$.ajax({
type: "POST",
url: "Home/GetUsers",
data: dataString,
success: function (data) {                      
     $.each(data.users, function(i, item) {
          $("#results").append('<p>' + item.Name + '</p>');
     });
}

I either get undefined, or nothing happens.

I get Object object if I do an alert(data) so I know it's returning something.

Obviously I'm doing something wrong here, so any help would be greatly appreciated.

Thanks!

8
  • Which method are you calling to get the data? You might need to manually convert data into an object. Commented May 26, 2011 at 21:14
  • Just a RESTful method in asp.net mvc Commented May 26, 2011 at 21:15
  • I mean which jquery method are you using to make the call? Commented May 26, 2011 at 21:16
  • Updated with the full .ajax call Commented May 26, 2011 at 21:20
  • 1
    I'm reasonably sure data is coming back as a string, trying doing something like var mydata = $.parseJSON(data) and alert mydata. You could also specify the dataType to be JSON and jQuery will try to convert it for you. Commented May 26, 2011 at 21:25

2 Answers 2

1

alert(data.users) shows you undefined? Try adding dataType: 'text', and then do alert(data) and check you're getting the right json :/

This is working for me:

var data = { "dateTime" : "5/26/2011",
             "requestTime" : 0.1020102,
             "users" : [ { "Email" : "[email protected]",
                "Location" : "home",
                "Name" : "Joe M",
                "UserId" : "42a7eae4-d4fe-49a3-93df-cd0cf219ac95"
              },
              { "Email" : "[email protected]",
                "Location" : "Work",
                "Name" : "Test Name",
                "UserId" : "97a444fb-6e3d-482c-a966-dbd3e0c739c8"
              }
            ]
           };
$(data.users).each(function(i, item) {
    $("#results").append('<p>' + item.Name + '</p>');
});
Sign up to request clarification or add additional context in comments.

2 Comments

must be something else than because I keep getting undefined when I do an alert(data.users).
After setting dataType: 'text', do alert() to data (not data.users), so you can see the full result.
1

You could also try doing something like this:

$.post('Home/GetUsers', dataString, function(result) {
  $(result.users).each(function(i, item){
            $('#results').append('<p>'+item.Name+'</p>');
            });
  },'json'
});

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.