On my page, I have one input
that the user fills out.
Then, in my JS, I have an object, like so:
var obj = {
sort: 'newest',
num: 10,
values: [1, 2, 3, 4]
};
As you can see, the object contains various types (a string, an integer, and an array). I need to pass both the input
value and the object's data to Laravel under a post request with the original type (i.e. string, integer, array).
But currently, all the data is being passed as a string:
https://i.stack.imgur.com/y6NGU.png
Here is my current code:
$('form').on('submit', function (event)
{
event.preventDefault();
var form = $(this);
var data = form.serializeArray();
$.each(obj, function (key, val)
{
data.push({ name: key, value: val });
});
$.ajax({
url: 'https://website.com/save',
type: 'post',
data: data,
dataType: 'json',
success: function (data)
{
//
}
});
});
What should I change to pass the data as their respective types?