簡體   English   中英

jQuery在ajax調用中發送表單數據和json對象

[英]jquery sending form data and a json object in an ajax call

我正在呼叫另一個ajax頁面,該呼叫發布了一個json對象。 我還需要從表單發送數據(不使用提交-我將ajax調用附加到使用e.preventDeault()的按鈕上)。

呼叫如下:

var myUrl = 'sendswatch-data.php';
            $.ajax({
                url: myUrl,
                data: {'swatchid[]':swatchArray}, 'formdata':$('#orderData').serialize()},
                type: "POST",
                error: function(xhr, statusText, errorThrown){
                    // Work out what the error was and display the appropriate message
                },
                success: function(myData){
                    $('#tabsampleorder').html(myData);
                    $('.tabber').hide();
                    $('#tabsampleorder').show();
                }
            });

我在formdata的頁面ID上有一個表單。

我如何發送這個以及json對象? 我試過了

data: {'swatchid[]':swatchArray}, 'formdata':$('#orderData').serialize()},

但這會產生一個錯誤。

在watchArray之后還有一個額外的}。 嘗試刪除它。

data: {'swatchid[]':swatchArray, 'formdata':$('#orderData').serialize()},

您可以從表單發送數據,如下所示:

data : { swatchid: swatchArray, formdata: $('#orderData').serialize() } 

對於添加的每個字段,您將在控制器中需要一個參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM