跨域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
这样就达到了我要求的效果。
- 跨域jsonp传数据,链接Highstock 及对象数组转 数组数组
- 对象数组数据
- java字符串及数组转json对象
- 数组进阶对象数组
- JS数组及面向对象
- 对象 数组及表格处理
- 数组及多维数组
- 数组及数组操作
- 对象数组排序&&对象数组去除重复数据
- 对象数组转JSON
- 对象 数组 字符串转
- 数组转对象
- json转对象数组
- 对象数组
- 对象数组
- 对象数组
- 对象数组
- 数组对象
- <meta name="format-detection" content="telephone=no">等
- flex 中embed用法
- 挑战drupal(1)
- E2238 Multiple declaration for 'fd_set' 报错
- Introducing VMware NSX – The Platform For Network Virtualization
- 跨域jsonp传数据,链接Highstock 及对象数组转 数组数组
- java Collections 的用法
- 内部类(多继承)
- 关于自己的学习和工作效率
- 内部类(再论工厂模式)
- NDK 学习
- @Autowired注解
- action中获取jsp页面的参数的方法
- Oracle_undo与redo