I made a function in jQuery that will get some data form page and then it need to send data with ajax.
I have tested with sending some string as data and all is working fine but now , when i need to send array for some reason it does not work.
On console i have:
["3"]
[]
And i dont get alert. Could be some problem with data types ?
$("#save").click(function() {
var sendArr= new Array();
var arr = $(".sortable").map(function(){return this.innerHTML}).get();
for (var i = 0; i < arr.length; i++) {
test =$(arr[i]).map(function() { return $(this).attr("data-ex"); });
console.log(test);
sendArr[i]=test;
}
$.ajax({
type: "POST",
url: "http://localhost/someurl/",
data: {action: sendArr}
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
gogic – make a simpler example to test with, and then build from that.
dataType is what you expect to receive; contentType is what you are sending. Your JSON has to be serialized with JSON.stringify(obj).
Also, when you send an array as a JSON, it should look like:
Use console.log to make sure the JSON you are creating is valid.