跨域jsonp传数据,链接Highstock 及对象数组转 数组数组

来源:互联网 发布:怎么导入天刀捏脸数据 编辑:程序博客网 时间:2024/06/11 15:50

(本做手机游戏,临时做了下公司发展的东西,所以是js新手,已解决问题为主,如果有效率意见或者更好的方法,欢迎提出来)
我的服务器端是 node.js+express+extjs

        数据库用的 mysql

               要连接 highstock (JQuery)

       1.  链接Highstock接口

               在HighStock的spline例子中留的接口是  
 $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) 
               我的接口是
 $.getJSON('http://localhost:3000/online?dataname='+dataname+'&start =0&limit=1000&callback=?', function(data) 

           这个链接直接对应的是extjs-demo/routes/index.js

app.get(/online,function(req,res){     query="limit" +req.query.start+","+req.query.limit     dataName=req.query.dataname    //Online = require('../models/online.js');     Online.get(dataName,query,function(err,data){     res.json({success:true,data:data;})})})

有数据但是没法对应callback,highstock.htm的function(data)得不到数据。

解决:

          json传数据

   

app.get(/online,function(req,res){     query="limit" +req.query.start+","+req.query.limit     dataName=req.query.dataname    //Online = require('../models/online.js');     Online.get(dataName,query,function(err,data){    // res.json({success:true,data:data;})       res.contentType('json');       res.sent('callback'+‘(’+JSON.stringify(data)+‘)’);})})

        会提示无法接收,json数据没法过去,后来  湖南-前端后端 http://www.xialeistudio.net/(透视ExtJS  256700289 QQ群的),说jsonp能解决问题,并给了一个链接http://www.xialeistudio.net/Article/90.shtml。说实话没明白。
       
         然后我就找http://www.cnblogs.com/lengyuhong/archive/2012/03/20/2370688.html这个们写的文章,将json转成了jsonp发送方式
 

app.get(/online,function(req,res){     query="limit" +req.query.start+","+req.query.limit     dataName=req.query.dataname    /*add*/    //urllib=require('url');    var params = urllib.parse(req.url,true);    //Online = require('../models/online.js');     Online.get(dataName,query,function(err,data){    // res.json({success:true,data:data;})       res.contentType('json');      // res.sent('callback'+‘(’+JSON.stringify(data)+‘)’);            res.end(params.query.callback+'('+JSON.stringify(data)+')');

至此为止,接口就对接上了,用http://localhost:3000/online?dataname=realonline&start=0&limit=1000&callback= 链接看了下数据结果。都是

{{xxx},{xx}...}对象形式,jsonp传过去以后被解析为1000个object.



     2.   对象转数组

我要的是

[                       [1234567890,5],                       [1234567891.6],                      ...                ]

但现在

{             {"timestamp":1234567890,"realonline":5},              {"timestamp":1234567891,"realonline":6},                      ...}

解决:

我暂时的解决办法)

   思路:遍历对象数组,然后把数值抽取出来存到一个arr1数组中,然后将arr1放到 resultArr中,最后将resultArr用jsonp传输过去就可以了


app.get(/online,function(req,res){     query="limit" +req.query.start+","+req.query.limit     dataName=req.query.dataname    /*add*/    //urllib=require('url');    var params = urllib.parse(req.url,true);    //Online = require('../models/online.js');     Online.get(dataName,query,function(err,data){       var arr=[];//用于保存jsonp传输的最终结果       //遍历data数组,从model/online.js处传过来的就是对象数组形式。              for(var i=0;i<data.length;++i){           var insertArr=[];           insertArr.push(data[i]['timestamp']);           insertArr.push(data[i][dataname]);//这个就是字符串为realonine的变量,用变量因为还要加载别的。            arr.push(insertArr);             //不知道javascript局部变量要不要释放,不会      }    // res.json({success:true,data:data;})       res.contentType('json');      // res.sent('callback'+‘(’+JSON.stringify(data)+‘)’);            res.end(params.query.callback+'('+JSON.stringify(arr)+')'); //有变动data->arr

这样就达到了我要求的效果。



(如果有任何Js指导意见,直说无妨,学习哈,这篇文章只限于解决问题,关于内存管理,时间效率等我都没有考虑,因为是N把手,慢慢来)