使用ajax导入JS数组以传递给JQuery中的函数/插件 [英] Import a JS array using ajax to pass though to a function/plugin in JQuery

查看:91
本文介绍了使用ajax导入JS数组以传递给JQuery中的函数/插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成的数组,如果对其进行硬编码,则将该数组对象传递给用于精细处理的函数.

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

例如:

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

但是,我需要使它更具动态性,因此需要在外部生成数组并使用ajax导入.这就是我正在尝试的:

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将输出{text:'some',weight:10},{text:'thing',weight:8})

(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.

问题似乎是在工作版本中,JQCloud插件将数组作为对象接收:[object Object],[object Object]其中,作为我的ajax版本,它以字符串形式接收/发送:{text:'some',weight:10},{text:'thing',weight:8}

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}

是否可以导入数组并将其作为适当的数组而不是字符串传递给JQCloud函数/插件,或者将字符串转换为数组进行处理?

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?

非常感谢..

回覆以下两个答案;我应该指出,返回值似乎没有被识别为有效的JSON数据...

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

推荐答案

我想您应该先将数据变量进行JSON解析,然后再将其发送到插件:

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'

...到ajax函数调用中的settings参数.

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

这篇关于使用ajax导入JS数组以传递给JQuery中的函数/插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆