1

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.

3
  • 1
    you have a typo in second part of response, there should be a : instead of , in second object Commented Jul 30, 2009 at 18:06
  • there is a security risk in sending an array literal as a JSON response. Better to make it { items: [{"UserID": 1}, {"UserID": 324}] }. Then your javascript will work too. Commented Jul 30, 2009 at 18:24
  • Gabe, how can I add that Items key? Commented Jul 30, 2009 at 18:35

5 Answers 5

6

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);
});
Sign up to request clarification or add additional context in comments.

1 Comment

can I check for success of the ajax call? or its assumed?
2

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.

Comments

2

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

Comments

1

The JSON you posted is invalid:

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

Notice the comma on the second UserID.

Comments

1

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.)

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.