3

When sending data via POST or GET with jQuery you use for format { name:"value" } so I thought, is there a way to do it with this kind of code:

var postdata = array();
postdata['name'] = "data";
$.post("page.php", postdata, function(data)
{
    alert(data);
}

I tried this, and it doesn't seem to work. Is there a proper way to do this?

1 Answer 1

1

What you are trying to initialize is an object, not an array. You can initialize objects in two ways:

var postdata = {};

Or:

var postdata = new Object();

Then you can assign keys and values just like you intended:

postdata['name'] = "data";

Or:

postdata.name = "data";

You can also build your object in the initialization phase:

postdata = {
    name: "data"
}
Sign up to request clarification or add additional context in comments.

1 Comment

ahhh! haha i forgot to close the }); with ); with the previous IF statement :) sorry, works now :D:D

Your Answer

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.