douzuizhuo0587 2013-05-11 12:41 采纳率: 0%
浏览 338

如何通过ajax发送二维数组?

I've got this method for sending data to the server:

var points = [];
var coords = polyline.geometry.getCoordinates();
for (var i = 0; i < coords.length; i++)
{
    var x = (coords[i][0]).toFixed(4);
    var y = (coords[i][1]).toFixed(4);
    points[i] = [x,y];
}
$("form").on('submit',
    function(e){
        var data = $(this).serializeArray();
        console.log(points);
        data.push({name:'points[]',value:points});
        $.ajax(
            {
                url: '/admin/index/add-route',
                method: 'post',
                data: data,
                success: function(result) {
                    console.log('succcess');
                }
            }
        );
        console.log(data);
        e.preventDefault();
        return false;
    }
);

On the backend, I obtain a one-dimensional array with these values:

(var dump of $_POST['points'])
array (size=1)  0 => string '48.9225,24.6948,48.9220,24.7090' (length=31)
  • 写回答

4条回答 默认 最新

  • dsw7547 2013-05-11 12:48
    关注

    You don't need to use .serializeArray();

    Just do the following:

    var data = {points: points};
    

    and on the PHP side:

    $points = json_decode($_POST['points']);
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮