如何在ajax上传递带有表单序列化数据的附加数据?

时间:2014-12-05 05:11:16

标签: javascript jquery ajax

如何在ajax post方法上传递带有表格序列化数据的附加数据?。

下面是我用于ajax帖子的代码,

   $(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frm.serialize(),
        success: function (data) {
            alert(data);
        }
    });
    });

此处,如何使用additional_data表单数据传递serialize

4 个答案:

答案 0 :(得分:1)

您需要将元素推送到现有的序列化数据。

var frmData = frm.serialize();
frmData.push({name: nameofthevariable, value: valueofthevariable});
frmData.push({name: nameofthevariable2, value: valueofthevariable2});
frmData.push({name: nameofthevariable3, value: valueofthevariable3});
$(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frmData,
        success: function (data) {
            alert(data);
        }
    });
    });

答案 1 :(得分:1)

来自jQuery API DOCS

The .serializeArray() method creates a JavaScript array of objects

The .serialize() method creates a text string in standard URL-encoded notation.

我认为要使用push,我们需要使用serializeArray

尝试使用

var frmData = frm.serializeArray();
frmData.push({name: "name", value: "test"});


$(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frmData,
        success: function (data) {
            alert(data);
        }
    });
    });

答案 2 :(得分:0)

serialize()创建表单的查询字符串。因此,您可以在其中附加其他参数。

$(document).ready(function()
     {
       var additional_data=$("#extra_data").val();
        $.ajax({
        type: 'POST',
        url: 'send_mail.php',
        data: frm.serialize()+'&param1='+value1+'&param2='+value2,
        success: function (data) {
            alert(data);
        }
    });
    });

答案 3 :(得分:0)

serializearray()可用于发送其他参数。用于发送附加参数的PFB代码。

An uncaught Exception was encountered 
Type: RuntimeException
Message: Unable to connect to the database.
Filename: C:\wed\wed\system\database\DB_driver.php Line Number: 433
Backtrace:
File: C:\wed\wed\web\application\core\MY_Controller.php Line: 10
Function: __construct
File: C:\wed\wed\web\application\controllers\sanjeh.php Line: 12
Function: __construct
File: C:\wed\wed\web\index.php Line: 317
Function: require_once

Ajax调用

var request = $('form').serializeArray();
request.push({name: "kindOf", value: "save"});