使用jquery ajax发送关联数组

时间:2014-04-17 05:56:50

标签: javascript jquery

我有关联数组。

var arr =   Array();
arr['a']    =   123;
arr['b']    =   234;
arr['c']    =   345;
arr['d']    =   456;
arr['e']    =   567;

我想用jquery ajax发送这个数组。这不是我的形式所以当我点击提交按钮然后它应该以我的形式出现,以便我可以发布关联数组数据

<form id="sendProposalForm" action="<?php echo frontpath(FILENAME_AJAXFILE);?>" method="post">

<input type="hidden" name="action" value="sendRequestProcess" />      
<input type="hidden" name="durationHidden" id="durationHidden" value="0"  />
<input type="hidden" name="priceSenstivityHidden" id="priceSenstivityHidden" value="0"/>
<input type="hidden" name="responseRateHidden" id="responseRateHidden" value="10" />
<input type="hidden" name="addTagsHidden" id="addTagsHidden" value=""   />
<input type="hidden" name="addAttachmentHidden" id="addAttachmentHidden" value=""   />
<input type="text" name="attachmentOrignalNameHidden" id="attachmentOrignalNameHidden" value="" />

$.ajax({url    :    "<?php echo frontpath(FILENAME_AJAXFILE);?>",
    type   :    "post",
    data   :    $('#sendProposalForm').serialize(),
    success:function(res) {                 

    }
  });

2 个答案:

答案 0 :(得分:6)

试试这个

var arr = Array();
arr['a'] = 123;
arr['b'] = 234;
arr['c'] = 345;
arr['d'] = 456;
arr['e'] = 567;

var obj = $.extend({}, arr); /* convert your array to an object */

$.ajax({url: "<?php echo frontpath(FILENAME_AJAXFILE);?>",
    type: "post",
    data: $('#sendProposalForm').serializeArray() + '&' + $.param(obj), /* merge your form with the new obj (your array) */
    success: function (res) {
        alert('SUCCESS!');
    }
});

答案 1 :(得分:1)

那有什么不对?你不能只将这个数组添加到Ajax请求中吗?

data: {
     formData: $('#sendProposalForm').serialize(),
     assocArray: '<?php echo json_encode($arr); ?>'
}