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

vondip
  • 13,809
  • 27
  • 100
  • 156
  • 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.) – JasCav Mar 12 '11 at 22:03
  • this is what I see when performing console.log(data) – vondip Mar 12 '11 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. – iivel Mar 12 '11 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. – Vinnyq12 Mar 12 '11 at 23:50
  • @Vinnyq12, when I write the value down manually it works. – vondip Mar 13 '11 at 05:45
  • @iivel, d.data.song returns an exception of undefiend – vondip Mar 13 '11 at 05:46
  • *"d.data.song returns an exception of undefiend"* Of course it does. What does `data.d.songs` return? – T.J. Crowder Mar 14 '11 at 16:35
  • @T.J. Crowder - undefined as well – vondip Mar 17 '11 at 04:48
  • 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. – T.J. Crowder Mar 17 '11 at 07:08

2 Answers2

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);
        }
Diesel337
  • 66
  • 8
0

Relevant information on how to do this here and here.

Community
  • 1
  • 1
Case
  • 1,848
  • 21
  • 32
  • 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. – vondip Mar 17 '11 at 04:49