I need to pass in a javascript array into jquery but my code is only returning [object object]
$( "#enviar" ).click(function() {
var data = {};
var i = 0;
var u = -1;
$.each($('.ui'), function() {
data[i] = this.value;
i++;
});
console.log(data);
$.ajax({
type: "POST",
url: "send_car.php",
data: "photo="+data+"&u="+u,
success: function(data){
}//end success
})//end ajax submit*/
});
console log is showing the array
Object {0="d-170", 1="d-171"}
but the data is being pass as [object object]
ui class is a hidden input with the same name and different values.
I have read various post and tried with serialize but I get the same results.
1 Answer 1
You're concatenating an object and a string, but jQuery supports passing in arrays and objects directly, so just use that
$.ajax({
type: "POST",
url: "send_car.php",
data: {
photo : data,
u : u
},
success: function(data){
}
});
what you're doing is
var obj = {key : "value"};
var data = "test" + obj;
and when you add an object to a string, they both become strings, and the string representation of an object is [object, Object].