【问题标题】:Post php array to using .post in javascript/jquery发布 php 数组以在 javascript/jquery 中使用 .post
【发布时间】:2013-11-15 16:49:54
【问题描述】:
如何将以下获取请求更改为 jquery 中的帖子?
$.getJSON('chartHelperphp?start=' + Math.round(e.min) +
'&end=' + Math.round(e.max) +
'&callback=?&array=<?php echo json_encode($data); ?>', function (data) {
chart.series[0].setData(data);
chart.hideLoading();
});
数组很大,我需要一种更高效的方式来传递数组。
【问题讨论】:
标签:
javascript
php
jquery
ajax
arrays
【解决方案1】:
如果服务器端已经有了数据,为什么还要从客户端传回服务器呢?
get 请求不能那么大,请尝试执行 POST 请求。
【解决方案2】:
$.post('chartHelperphp?start=' + Math.round(e.min) +
'&end=' + Math.round(e.max) +
'&callback=?&array=<?php echo json_encode($data); ?>', function (data) {
chart.series[0].setData(data);
chart.hideLoading();
} , 'json');
应该这样做
【解决方案3】:
您需要使用$.post(或$.ajax)方法而不是$.getJSON。
$.post('chartHelperphp', {
start: Math.round(e.min),
end: Math.round(e.max),
array: <?php echo json_encode($data); ?>
}, function(data) {
// do something with data
}, 'json');
当然,这是假设资源托管在同一个 URL 上,并且您不需要 JSONP。如果您使用的是 JSONP,则无法 POST 数据(除非服务器支持 CORS)。