dou12352 2014-06-05 12:57
浏览 62
已采纳

没有通过JSON获取动力学js中的数据

i am stuck in passing data from php to kineticjs throught json below is the code

$arrParams = array('images' =>$images, 'captions' => $content);
init({"container":"slider","data":<?php echo json_encode($arrParams); ?>});

i want to pass the above Json array in my below given coded kineticjs code

var data;
var canvasWidth;
var canvasHeight;
var stage;
var layer = new Kinetic.Layer();
var imageNodes = [];
var imageObjects = [];
var imageBox = [];
var captionBox = [];
var textNodes = [];
var totalObjects = 0;
var objectLoaded = 0;

var currentSlide = false;
var defaultPosition = [];
var rightCardPosition = [];
var leftCardPosition = [];

function init(config) {
    canvasWidth = config.width | 600;
    canvasHeight = config.height | 300;

    defaultPosition["x"] = canvasWidth / 4;
    defaultPosition["y"] = canvasHeight + 5;

    leftCardPosition["start"] = [];
    leftCardPosition["end"] = [];
    leftCardPosition["start"]["x"] = (canvasWidth / 3) - 180;
    leftCardPosition["start"]["y"] = canvasHeight - 260;
    leftCardPosition["end"]["x"] = (canvasWidth + 100);
    leftCardPosition["end"]["y"] = -250;

    rightCardPosition["start"] = [];
    rightCardPosition["end"] = [];
    rightCardPosition["start"]["x"] = (canvasWidth / 3) + 160;
    rightCardPosition["start"]["y"] = canvasHeight - 300;
    rightCardPosition["end"]["x"] = (canvasWidth + 100);
    rightCardPosition["end"]["y"] = -250;

    stage = new Kinetic.Stage({
        container: config.container,
        width: canvasWidth,
        height: canvasHeight
    });

    totalObjects = data.images.length;
    for (var i = 0; i < data.images.length; i++) {
        totalObjects += data.captions[i].length;
    }
    for (var i = 0; i < data.images.length; i++) {
        loadImage(data.images[i]);
        loadCaption(data.captions[i]);
    }
}

I am getting error like in the line

totalObjects = data.images.length;

data is undefine... which means images is not pass from the json array

  • 写回答

1条回答 默认 最新

  • dozrhldy285751 2014-06-06 12:21
    关注

    You're encoding the JSON with PHP, now you just need to parse it with JavaScript and store it in your data variable.

    data = JSON.parse(config.data);
    

    Put this at the start of your init function and it should work for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?