2

My HTML page allows the user to create a list of items that get appended to a table. For simplicity lets say each item has 2 things. 1) a description and 2) a price. I also add this list of items into an object that I want to submit to a server. An example of an object created with 3 items would looks like

myObject = {
    "Item1":["description1", "price1"],
    "Item2":["description2", "price2"],
    "Item3":["description3", "price3]
}

What is the correct way to submit this data to the server. The only way I am sure of is to append this object to a hidden form. Is there another way.

I am using nodejs with an expressjs framework if that helps. I then input this into a MySQL database by iterating over the object. Thank you.

4
  • 4
    Using AJAX comes to mind, also JSON.stringify(). Commented May 22, 2013 at 2:03
  • yes ajax with json should sort you out. Commented May 22, 2013 at 2:04
  • In case you've never used Ajax, MDN's Ajax tutorial might be helpful. Commented May 22, 2013 at 2:05
  • O perfect, Ajax it is. I have not used it Yet, thank you for the advice and that guide as well. I appreciate it. All google attempts for best ways of submitting data failed to explain why and when to do what. Commented May 22, 2013 at 2:08

2 Answers 2

1

Using JQuery:

var obj = { ... };

$.ajax({
  type: "POST",
  url: "your/url",
  data: obj
});
Sign up to request clarification or add additional context in comments.

3 Comments

The question doesn't mention jquery.
To expand on the comment by @Jack, in general, please don't post jQuery answers to non-jQuery questions unless either: 1) jQuery offers functionality (or a plugin) that would be significantly onerous to reproduce with plain JS, or 2) your answer offers a jQuery solution as a supplement or alternative to a non-jQuery solution.
As you can see above, the question author said he doesn't know AJAX. And this is exactly what I thought, so, I didn't want to complicate my answer by implementing mannualy a JSON Post request, I decided to explain this in the simplest way as possible, using JQuery. I agree (not totally) with you by not suggesting JQuery answers when not explicitly asked. But when the library increase simplicity and the user appears to be a begginner in JS, my opinion is that there's no real problem.
1

Try this: JSON.stringify(yourData)

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.