1

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.

asked Apr 24, 2015 at 20:06

1 Answer 1

3

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].

answered Apr 24, 2015 at 20:08
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.