0

I have a generated array which if hard coded passes the array objects to a function for processing fine.

For example:

$("#termCloud").jQCloud([{text:'some',weight:10},{text:'thing',weight:8}]);

However, I need to make this more dynamic so am generating the the array externally and importing using ajax. This is what I'm Trying:

(generateArray.asp would output {text:'some',weight:10},{text:'thing',weight:8})

$.ajax({
    url: '/generateArray.asp',
    success: function(data){
        $("#wordCloud").jQCloud([data]);
    }
})

I have tried several dataTypes and all fail.

The problem seems to be that the in the working version the JQCloud plugin receives the array as objects: [object Object],[object Object] where as my ajax version receives/sends it as a string: {text:'some',weight:10},{text:'thing',weight:8}

Is there a way to import the the array and pass it though to the JQCloud function/plugin as a proper array rather than a string or convert the string to an array for processing?

Many thanks..

In repospone to two answers below; I should point out that the return doesn't seem to be recognised as valid JSON data...

JustinReid
  • 192
  • 10

2 Answers2

0

I guess you should JSON-parse the data variable before sending it to the plugin:

var json = JSON.parse(data);
$("#wordCloud").jQCloud([json]);

...or you could add

dataType : 'json'

...to the settings parameter in the ajax function call.

Jørgen
  • 8,820
  • 9
  • 47
  • 67
  • If you mean `$.parseJSON(data)` I tried that (sorry should have said) - Firebug reporting that it's invalid JSON – JustinReid Aug 10 '11 at 11:40
  • Unfortunatly the `dataType:'json'` fails, not convinced that the return is valid JSON or that JQuery can read it as such.. – JustinReid Aug 10 '11 at 13:16
  • So it seems. I tried parsing the string in Firebug and it returned invalid JSON. I got it correct by changing the single quotes to double quotes: var foo = $.parseJSON('[{"text":"some","weight":10},{"text":"thing","weight":8}]'); I didn't know this, but it seems to be covered here: http://stackoverflow.com/questions/2275359/jquery-single-quote-in-json-response – Jørgen Aug 11 '11 at 08:11
  • Fantastic, thanks for that @Jørgen - I had reverted to splitting the returning string and rebuilding the array - this is much better :-) Superb and thanks again... – JustinReid Aug 11 '11 at 09:09
0

Try:

success: function(data){
        $("#wordCloud").jQCloud([{text: data[0].text, weight: data[0].weight}, {text: data[1].text, weight: data[1].weight}]);
}

The response is automatically converted to Objects by the $.Ajax() function, as it is a json-string.

Swiftaxe
  • 172
  • 1
  • 3
  • Thanks for the reply - wasn't sure about the whole JSON string thing as previous attempts to parseJSON or set the dataType to JSON failed. Sure enough Objects were created but unfornatley the values are undefined so it fails anyhow. I'd hazard a guess (not knowing too much about JSON) that the return is not valid JSON, or at least not that JQuery can decipher – JustinReid Aug 10 '11 at 13:14