views:

245

answers:

5

My Json response looks like:

[{"UserID": 1}, {"UserID", 324}]

I call the page to get json like:

$.get("myurl.aspx", {blah:1}, function(data) {

       $.each(data.items, function(i, item) {
             alert(item.UserID);

       });

});

Firebug is reporting the error:

G is undefined.

+2  A: 

It appears your response is not a true JSON object. Notice that there is a comma instead of a colon in your response.

If that's just a typo, check the request and response in the Firebug console (on the Net tab) to see what data is being sent to the myurl.aspx page. You should see your AJAX request, as well as the data that is being sent back to your page.

Tim S. Van Haren
+5  A: 

I think you want this instead since your data var doesn't have a property called items:

$.each(data, function(i, item) {
     alert(item.UserID);
});
John Sheehan
can I check for success of the ajax call? or its assumed?
mrblah
+1  A: 

The JSON you posted is invalid:

[{"UserID": 1}, {"UserID", 324}]

Notice the comma on the second UserID.

CMS
+2  A: 

Don't forget to pass a data type parameter to get so that it knows to expect JSON, or use getJSON instead.

bryan
A: 

By default, ASP.Net encapsulates the JSON object in another object named 'd'. Your response would look like this then:

{"d": [{"UserID": 1}, {"UserID": 324}]}

Try this:

$.get("myurl.aspx", {blah:1}, function(data) {

   $.each(data.d, function(i, item) {
         alert(item.UserID);
   });
});

Check out this link for Microsoft's reasoning (bottom of the page.)

Ben Koehler