0

A newbie question from me, please.

I have a large data set that I prefer to be submitted as an object instead of an array; e.g.

foo = bar = baz = {};
$.ajax({
    url: "index",
    type: "post",
    data: { foo: foo, bar: bar, baz: baz },
    dataType: "json"
});

Upon submit, Firebug tells me that I have submitted:

bar [object Object]
baz [object Object]
foo [object Object]

What I want is to be able to access the contents of foo, bar, baz (contrived example, of course).

Is this possible in Javascript? Or do I need to use arrays, which I do not prefer?

2 Answers 2

3

bar, baz, and foo in your example are empty objects, the [object] nonsense is JavaScript doing its best to turn the variables into strings. I suggest including something that will turn your objects into JSON strings. See json.org and specifically json2.js and then call the stringify() method on each of them:

foo = bar = baz = {};
$.ajax({
    url: "index",
    type: "post",
    data: { foo: foo.stringify(), bar: bar.stringify(), baz: baz.stringify() },
    dataType: "json"
});
Sign up to request clarification or add additional context in comments.

Comments

2

No. Everything that is submitted via HTTP is basically a string or an array of strings. You can use JSON to convert each object into a string representation, which may be what you want.

Comments

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.