最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SignalR, passing an object from the HUB to the Client (MVC C#) - Stack Overflow

programmeradmin6浏览0评论

I've looked around at other questions but they don't seem to fully answer this question, I'm trying to pass an object via JSON to the client Javascript. I'm using Newtonsoft.Json to make the process easier, but I can't seem to recieve the object.

Here's the code:

When a connection is made, I call the Hub using start().done() in the client javascript:

   //start m with server
$.connection.hub.start().done(function () {
    console.log('Grabbing playlist data');
    Playlist.server.requestPlaylist();
});

This calls the following method, which is supposed to grab the object and pass it back:

    public void requestPlaylist()
   {
       var playlistData = (from c in db.Playlist where c.ID > 0 select c).Include(h => h.Song).ToList();
       Playlist player = new Playlist();

       foreach (var item in playlistData)
       {
           player.ID = item.ID;
           player.Profile = item.Profile;
           player.Song.ID = item.Song.ID;
           player.Song.name = item.Song.name;
           player.upvotes = item.upvotes;
       }

       string jsonObject = JsonConvert.SerializeObject(player);


       Clients.All.recievePlaylist(jsonObject);
   }

SO here, I'm searching the database, getting the results and storing it into the playlist model, then using newtonsoft.json to convert the model into a json object (Its roughly the same principle they have as an example on their site).

The client javascript that is invoked from this is:

function recievePlaylist(jsonObject) {
    console.log('test to recieve data: ' + jsonObject.ID + ' test.');

};

Now just for testing purposes I'm just logging out out to the console, but this e back with nothing:

"test to recieve data: test." is how it es back.

What am I missing?

I've looked around at other questions but they don't seem to fully answer this question, I'm trying to pass an object via JSON to the client Javascript. I'm using Newtonsoft.Json to make the process easier, but I can't seem to recieve the object.

Here's the code:

When a connection is made, I call the Hub using start().done() in the client javascript:

   //start m with server
$.connection.hub.start().done(function () {
    console.log('Grabbing playlist data');
    Playlist.server.requestPlaylist();
});

This calls the following method, which is supposed to grab the object and pass it back:

    public void requestPlaylist()
   {
       var playlistData = (from c in db.Playlist where c.ID > 0 select c).Include(h => h.Song).ToList();
       Playlist player = new Playlist();

       foreach (var item in playlistData)
       {
           player.ID = item.ID;
           player.Profile = item.Profile;
           player.Song.ID = item.Song.ID;
           player.Song.name = item.Song.name;
           player.upvotes = item.upvotes;
       }

       string jsonObject = JsonConvert.SerializeObject(player);


       Clients.All.recievePlaylist(jsonObject);
   }

SO here, I'm searching the database, getting the results and storing it into the playlist model, then using newtonsoft.json to convert the model into a json object (Its roughly the same principle they have as an example on their site).

The client javascript that is invoked from this is:

function recievePlaylist(jsonObject) {
    console.log('test to recieve data: ' + jsonObject.ID + ' test.');

};

Now just for testing purposes I'm just logging out out to the console, but this e back with nothing:

"test to recieve data: test." is how it es back.

What am I missing?

Share Improve this question asked Jul 19, 2014 at 12:10 JayJay 1,0331 gold badge20 silver badges32 bronze badges 3
  • Don't you have to eval(jsonObject) first in receivePlaylist()? – ovm Commented Jul 19, 2014 at 12:31
  • 1 Have you looked at jsonObject in your recievePlaylist function? Does it containt anything at all? – Ilya Luzyanin Commented Jul 19, 2014 at 12:32
  • 2 BTW, your "player" object gets overwritten multiple times in foreach loop - are you sure that is what you planned? – Ilya Luzyanin Commented Jul 19, 2014 at 12:41
Add a ment  | 

2 Answers 2

Reset to default 6

Because you convert the object to a string on the server before passing it to the client, the client receives a string. Your string representation of a json object doesnt have an ID property so the value will be "undefined".

On the client you can use this to convert the string to a json object:

jsonObject = JSON.parse(jsonObject);

Just add that line to the top of your recievePlaylist function.

Note: You shouldn't actually need to convert your server object to a json string on the server side. SignalR automatically converts your server side objects to json objects on the client.

If you call WebAPI and receive json response/result on client side (JavaScript/jQuery). The way is general for both SignalR or WebAPI in regards of parse jsone response and the use it as object.

var obj = jQuery.parseJSON( '{ "name": "John" }' );
alert( obj.name === "John" );
发布评论

评论列表(0)

  1. 暂无评论