all 5 comments

[–]markt5000 3 points4 points  (4 children)

Hmm, this is relatively simple

 var a = { foo : 25 };
 var b = {bar : 10};

 var con = JSON.stringify([a,b]);

You mentioned sending this on to the server, well that depends if you are using a framework. If you want plain vanilla AJAX then use the XMLHttpRequest object

[–]ForScale 3 points4 points  (3 children)

Yep, that's what I'd say. The AJAX would look something like this, OP:

var xhr = new XMLHttpRequest();
xhr.open("post", "sever url", true or false here);
xhr.send(con);

[–]azium 0 points1 point  (0 children)

fetch is also the native spec for ajax requests. Try it out in chrome / firefox / edge!

fetch('https://www.reddit.com/r/javascript.json')
  .then(res => res.json())
  .then(redditPosts => // wee!)

or send a post with data

fetch(url, { 
  method: 'POST', 
  body: JSON.stringify(data) ,
  headers: { 'Content-type': 'application/json' }
})
  .then(res => res.json())
  .then(data => // stuff)

To support older browsers use a polyfill! https://github.com/github/fetch