繁体   English   中英

将php数组发布为在javascript / jquery中使用.post

[英]Post php array to using .post in javascript/jquery

如何将以下get请求更改为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();
});

数组很大,我需要一种更有效的方法来传递数组。

如果您已经在服务器端拥有数据,为什么还要将其从客户端传递回服务器?

获取请求不能太大,请尝试执行POST请求。

$.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');

应该做

您需要使用$.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,则无法发布数据(除非服务器支持CORS)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM