0

I'm making a call to another ajax page, the call posts a json object. I also need to send data from a form (not using submit - I have the ajax call attached to a button which uses e.preventDeault()).

The call is as folows:

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();
                }
            });

I have a form on the page id of formdata.

How do I send this as well as the json object? I've tried

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

but that's generating an error.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dizzy Bryan High
  • 2,015
  • 9
  • 38
  • 61

2 Answers2

5

You have an extra } after watchArray. Try removing that.

data: {'swatchid[]':swatchArray, 'formdata':$('#orderData').serialize()},
Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
  • Is there any way to do this that doesn't require you to explicitly name the field name ('formdata') as a key? – zakdances Oct 16 '11 at 00:49
2

You can send data from the form as follows:

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

You will need a parameter in the controller for every field that your add.

Leons
  • 2,679
  • 1
  • 21
  • 25