0

I am building a website in asp.net mvc and jquery. I am parsing json objects and sending them from server side to client side. From some reason when I try calling the object it doesn't work. Here is how one of my objects looks when calling console.log(data) on the jquery callback

{
"songs": [
{
     "SongId": 1,
     "Name": "Black Eyed Peas - The Time (Dirty Bits)",
     "VideoID": "D7K3wFXJFsQ",
     "LastUpdated": "/Date(1299951907000)/"
},
{
     "SongId": 2,
     "Name": "Paramore - The Only Exception",
     "VideoID": "-J7J_IWUhls",
     "LastUpdated": "/Date(1299951907000)/"
  }
 ]
}

and here is how I try calling it:

console.log(data.songs)

Edit 1 When trying to specy the value as string, right in the client side (writing the string as literal), it works fine.

On the server side I am using JsonResult as the return type.

This keeps giving me an undefined value. Why?

Thank you

9
  • 3
    Can you show us the code for how you are sending the JSON to the client (your controller method) as well as a more significant portion of the view method? (Your error message indicates that data.songs isn't ever receiving the value. I don't believe your problem has anything to do with the formatting of the data.) Commented Mar 12, 2011 at 22:03
  • this is what I see when performing console.log(data) Commented Mar 12, 2011 at 22:05
  • 3
    I'm assuming you're using .NET 3.5? If so, keep in mind that all response objects are wrapped in "d" as a preventative measure for JSON hacks. Try and see what is in d.data.song? Otherwise we'll need more code. Commented Mar 12, 2011 at 22:29
  • 1
    break it down, take the text above and put it into a js var yourself. If it works then you know there is something else, if it doesn't then start smartly removing bits until you find the culprit code. Hope this helps. Commented Mar 12, 2011 at 23:50
  • 2
    @vondip: The best thing is to use a debugger and place a breakpoint where you receive the data, then look at the data in the debugger. There are debuggers built into IE8, IE9, Chrome, Opera, and Safari; you can get Firebug for Firefox; and for testing with earlier versions of IE, there's a free edition of VS.Net that you can use. Commented Mar 17, 2011 at 7:08

2 Answers 2

0

Relevant information on how to do this here and here.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you Sharon, but this is not exactly my situation. I am having issues sending data from server side to client side, not the other way arround.
0

Below is a modified example of how i have done this in the past. I am not sure how your ActionReslut/JsonResult is coded, if you could provide an example it would help us help you.

So i hopefully the example below helps.

public ActionResult GetSongs()
        {
            var Songs = _session.All<Songs>()
                           .OrderBy(x => x.Song.Name)
                           .Select(x => new Song { SongId = x.Id, name = x.Name })
                           .ToList();

            return Json(Songs, JsonRequestBehavior.AllowGet);
        }

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.