I want to send an array as an Ajax request:
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
How can I do this?
I want to send an array as an Ajax request:
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
How can I do this?
info = [];
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
data: {info:info},
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
info=arrayasvalues
instead of every key is a param?
Sep 27, 2016 at 15:35
List<string>
and a string[]
parameter, but in both cases, the array being past ends up being one comma-delimited string in the first element of the List<string>
or string[]
param. Any thoughts ?
Just use the JSON.stringify method and pass it through as the "data" parameter for the $.ajax function, like follows:
$.ajax({
type: "POST",
url: "index.php",
dataType: "json",
data: JSON.stringify({ paramName: info }),
success: function(msg){
$('.answer').html(msg);
}
});
You just need to make sure you include the JSON2.js file in your page...
List<string>
and a string[]
parameter, but in both cases, the array being past ends up being one comma-delimited string in the first element of the List<string>
or string[]
param. Any thoughts?
NOTE: Doesn't work on newer versions of jQuery.
Since you are using jQuery please use it's seralize function to serialize data and then pass it into the data parameter of ajax call:
info[0] = 'hi';
info[1] = 'hello';
var data_to_send = $.serialize(info);
$.ajax({
type: "POST",
url: "index.php",
data: data_to_send,
success: function(msg){
$('.answer').html(msg);
}
});
jQuery.serialize()
. There is .serialize()
but it's meant to be used on a set of form elements. Try this code and you will only get TypeError: $.serialize is not a function
Aug 5, 2017 at 0:52