Import a JS array using ajax to pass though to a function/plugin in JQuery
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: functio开发者_如何学Gon(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...
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.
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.
精彩评论