jquery ajax send form.serialize, variable and array all as d


jquery ajax send form.serialize, variable and array all as data

我有下面的jquery AJAX,

var reply_content = $('#summernote_1').code();
$.ajax({
        type:"POST",
        url: "<?php echo base_url();?>index.php/test/send_reply/"+<?php echo $testinfo->test_id;?>,
        data: $("#test_fm").serialize()+'&file=' + file + '&test_subject=<?php echo $testinfo->testsubject;?>' +'&reply_content=' + reply_content,
});

我有文件变量是数组包含如下

array([0]=> a [1]=>b),

这是与我在 AJAX 中传递的其他函数一起返回的,

如何在 AJAX 中传递它?

    var reply_content = $('#summernote_1').code();
    $.ajax({
            type:"POST",
            url: "<?php echo base_url();?>index.php/test/send_reply/"+<?php echo $testinfo->test_id;?>,
            data: {$("#test_fm").serialize()+'&file=' + file + '&test_subject=<?php echo $testinfo->testsubject;?>' +'&reply_content=' + reply_content},
    });

您可以使用serilizeArray()

如果你在php中尝试它,那么你可以这样尝试:

$url = urlencode(serialize($your_array))

在数据中追加$url

data: {$("#test_fm").serialize()+"&file=<?php echo $url; ?>&test_subject=<?php echo $testinfo->testsubject;?>" +"&reply_content=" + reply_content}

并恢复您可以使用的变量

$arrVar = unserialize(urldecode($_POST['file']))
您可以使用

serializeArray()来创建也可以添加的对象。在可能的情况下将 php 与 js 分开以提高可读性也是一个好主意:

var url = "<?php echo base_url() . 'index.php/test/send_reply/' . $testinfo->test_id;?>";
var test_subject = "<?php echo $testinfo->testsubject;?>";
var data = $("#test_fm").serializeArray();
data.push(
    {
        file: file, 
        test_subject: test_subject
    }
);
$.ajax({
        type:"POST",
        url: url,
        data: data
});