weixin_33674437 2015-04-27 11:58 采纳率: 0%
浏览 323

通过AJAX提交表单数组

I have a form which I would like to submit via Ajax, however, part of it contains an array. I am having difficulty passing this array via Ajax. An example of my Ajax is below, where I would usually pass the form entry data via how it is below after data (one: $('#one').val()) where I would have one row of this for each field.

Now I have a new set of fields, where the information needs to be passed through as an array. I have tried using serialize and formData -- var fd = new FormData("#form") -- and so far either just this array has been passed through, or nothing from the form is passed through, or just the array is not passed through.

Can anyone please point me in the right direction?

$("#form").submit(
function() {

    if (confirm('Are you sure you want to edit this?')) {

        $("#formMessages").removeClass().addClass('alert alert-info').html(
            '<img src="images/loading.gif" /> Validating....').fadeIn(500);


        $.ajax({
            url: $("#form").attr('action'),
            dataType: 'json',
            type: 'POST',
            data: {
                one: $('#one').val(),
                two: $('#two').val()
            },

            success: function(data){
                //success stuff would be here
            }
        });

    }
    return false;
});

Thanks.

  • 写回答

2条回答 默认 最新

  • Memor.の 2015-04-27 12:08
    关注

    Have you tried this:

    Written in JavaScript:

    your_array = JSON.stringify(your_array);
    

    And in PHP:

    $array = json_encode($_POST['array']);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题