The Learner
The Learner

Reputation: 3927

Jquery sending the data using post

I am trying to send some data to the server.

on the server it should be going to www.sample.com/data2

I have a Array as arrays; var = arrays; this arrays has the value.

I am doing

$.post('/data2', {arrays});

This is not working... this is inside the .submit(). I can see in firebug it is giving Error for the variable as "invalid object initializer"

Upvotes: 0

Views: 42

Answers (2)

kieran
kieran

Reputation: 1537

Objects in javascript are essentially key-value mappings, which means that attempting to define an object literal with an unlabelled value (inside the {} block) will throw a syntax error.

You should instead provide a key for the data, e.g. {"data" : arrays}.

As usual the MDN pages on this facet of javascript is thorough and informative if you would like some further reading.

Upvotes: 1

anson
anson

Reputation: 4164

Im assuming your var arrays is

[1,2,3,etc..]

which you can't just wrap with {} brackets...you need to give a key to your value, such as..

{"arr" : arrays}

If your using PHP, your array would be held in the $_POST array in the index "arr", like...

$arr = $_POST['arr'];

Upvotes: 1

Related Questions