tags:

views:

63

answers:

4

Hello,

On page load, I do one call to get the current status of all the favourite links (display the right message aka: click to subscribe, click to unsubscribe.

Final Code! :) Thanks for your help guys, please check post revisions to see the issue.

$(InitFavorite);

function InitFavorite(){

    var jList = $(".favourite_link");
    var ids_to_check = {};//new Array();

    $.each(jList, function () {
        var id = this.id;
        var object = id.split("_");
        if (!ids_to_check[object[1]]) {
            ids_to_check[object[1]] = [];
        }
        ids_to_check[object[1]].push(object[0]);
    });

    $.ajax({
        type: 'POST',
        url: '/user/subscription/favourite-listing',
        data: ids_to_check,
        dataType: 'json',
         beforeSend: function(x) {
              if(x && x.overrideMimeType) {
               x.overrideMimeType("application/json;charset=UTF-8");
          }
         },
        error: function() {
            //could not load favourites
        },
        success: function(returned_values) {

            $.each(returned_values.favourites.Clip, function(i, item) {
                $('#'+i+'_Clip').html(''+item+'');
            });
            $.each(returned_values.favourites.Playlist, function(i, item) {
                $('#'+i+'_Playlist').html(''+item+'');
            });
        }
    });
A: 

refer this link JSON.org

kedar kamthe
A: 

You must parse your text so it becomes an object instead of JSON (plain-text):

var o = eval('(' + returned_values + ')');

then you should be able to use o as the object:

alert(o.env); // Gives "development"

more specifically:

    success: function(returned_values) {
        var o = eval('(' + returned_values + ')');
        $.each(o, function(i, item) {
            console.log(item);
        });
    }
Mickel
Just tried this, nothing happens, no alert and console.logging it returns nothing either
azz0r
see my edits...
Mickel
A: 

what jQuery version do you use? 1.4.x should convert your data into an object if it is well formed json.

So, either update to 1.4.2 or use

if(JSON)
   var obj = JSON.parse(returned_values);

again, make sure returned JSON has a valid format (see json.org)

if your browser does not support JSON, you have to go with eval();

Kind Regards

--Andy

jAndy
By doing: success: function(returned_values) {console.log(returned_values); Firebug returns: Object { env="development", more...}
azz0r
A: 

Brownie points for anyone who can suggest a way to stop IE6 going to the URL! :)

azz0r