Reputation: 733
I have a webpage that is sending data via ajax with data including text and also some variables that are arrays. My code is looking like this:
var data = {
'action':action,
'main_phase':main_phase,
'secondary_phase':secondary_phase,
'domain':domain,
'description':description,
'option':option,
'assumption':assumption,
'site':site_data,
'quantity':quantity,
'loe_per_quantity':loe_per_quantity,
'formula':formula,
'recurrent':recurrent,
'start_date':start_date,
'end_date':end_date,
'cons':cons_data
}
where site_data and cons_data that are arrays like this:
cons_data
0: [name: "business consultant", percentage: "0", price: "0"]
1: [name: "junior consultant", percentage: "50", price: "1000"]
2: [name: "lead consultant", percentage: "50", price: "1200"]
which is created like this:
var cons_data = [];
loe_data.col.cons.forEach(fill_cons_data_accept);
function fill_cons_data_accept (cons,index){
cons_data[index] = [];
cons_data[index].name = cons.name;
cons_data[index].percentage = $('input.loe_cons_percentage[data-name="'+cons.name+'"]').val();
cons_data[index].price = $('input.loe_cons_price[data-name="'+cons.name+'"]').val();
}
I m trying to send all this data to another page via ajax:
$.ajax({
type: 'post',
url: "{!! route('loeCreateUpdate','') !!}/"+id,
data:data,
dataType: 'json',
success: function(data) {
...
And when I check what I receive on the other side with:
public function create_update(Request $request,$id)
{
$inputs = $request->all();
return json_encode($inputs);
}
I get everything except the 2 arrays.
Here is what I find through console.log():
{
action: "update"
assumption: "The customer needs to come on site"
cons: (3) [Array(0), Array(0), Array(0)]
description: "Evaluate what the customer needs"
domain: "Security"
end_date: ""
formula: "{{#site}}+{{ape}}-2*{{#site}}"
loe_per_quantity: "5"
main_phase: "BUILD"
option: "option 1"
quantity: "2"
recurrent: 0
secondary_phase: "workshop"
site: [ape: Array(0), switches: Array(0), #site: Array(0)]
start_date: ""
}
{
action: "update"
assumption: "The customer needs to come on site"
description: "Evaluate what the customer needs"
domain: "Security"
end_date: null
formula: "{{#site}}+{{ape}}-2*{{#site}}"
loe_per_quantity: "5"
main_phase: "BUILD"
option: "option 1"
quantity: "2"
recurrent: "0"
secondary_phase: "workshop"
start_date: null
}
As you can see in the got back results, site and cons disappeared...
Upvotes: 0
Views: 117
Reputation: 12835
You can convert the arrays to json string before appending it to the data
const site = JSON.stringify(site_data);
const cons = JSON.stringify(cons_data);
data.site = site;
data.cons = cons;
Or you can use jQuery's serializeArray
const site = $(site_data).serializeArray();
const cons = $(cons_data).serializeArray();
data.site = site;
data.cons = cons;
Verify the jQuery syntax - not too sure about it
Upvotes: 1