I was wondering if its possible to pass data stored in a javascript array to the server using jQuery's ajax function..
In the jQuery documentation it specifies:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
can "data" be set to an array? How would this work given it seems data is expecting key value pairs? I currently just hard code the values but I want it to be a more dynamic approach..my current code is:
jQuery.ajax({
url: "/createtrips/updateitin",
type: 'POST',
data: {place1: 'Sydney', place2: 'London'},
dataType: 'json',
});
Ajax (Asynchronous JavaScript And XML) allows web pages to be updated asynchronously by exchanging data to and from the server. This means you can update parts of a web page without reloading the complete web page.
post() makes Ajax requests using the HTTP POST method. The basic syntax of these methods can be given with: $. get(URL, data, success); —Or— $.
$. post is a shorthand way of using $. ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code.
if you would like to send 2 array via ajax, you can create a new array like this: <? php echo json_encode(array( 'issues' => $issue, 'foo' => $foo, )); ?>
I created an array like this:
var placesfortrip = {};
then added to it like this:
placesfortrip["item"+counter] = inputVal;
(where counter
is an incremented counter variable)
then assigned this to the data
property of the ajax call
jQuery.ajax({
url: "/createtrips/updateitin",
type: 'POST',
data: placesfortrip,
dataType: 'json',
});
and if I look at the XHR tab in firebug it appears those values get posted!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With