Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I send arbitrary JSON to node.js without a page reload?
    primarykey
    data
    text
    <p>My ultimate goal is to send an arbitrary JSON to node.js when a button is clicked. I currently only know how to send input from a form. Here's some code I put together to send form information:</p> <pre><code>function postForm() { $('form').submit(function(e) { e.preventDefault(); // no page reload $.post( $(this).attr('action'), $(this).serialize(), function(data) { console.log('Code for handling response here.') }, 'json' ); }); } </code></pre> <p>Where the HTML looks like:</p> <pre><code>&lt;form action='/send' method='post'&gt; &lt;input name= "foo" type="radio" value=1&gt; &lt;input type="submit" value="Submit"&gt; &lt;/form&gt; </code></pre> <p>And the relevant express/node.js code looks like:</p> <pre><code>app.post('/send', function(request, response) { fs.appendFile('test.txt', JSON.stringify(request.body) + '\n', function(e) { if(e) throw e; console.log(request.body); }); }); </code></pre> <p>However, I don't know how to adapt this example to use data that is not from form input. To give context, I'm building a web-based user study, and I want to send various information collected about the user to node.js. I've tried variants of what was working for the form submission, but none of my attempts have been successful. My impression was that I could just swap out <code>$(this).serialize()</code> to any other data that the client can access, but I couldn't get this line of thought to work. I also tried altering some of the many <code>.ajax()</code> examples, but those always redirected the page which is undesirable, since my study will lose user-state information if the page refreshes.</p> <p>I've done decent amount of client and server side programming, but I have next to no knowledge about how ajax works, which is proving rather problematic for solving this! And also rather silly since, often times, that's what glues the two together :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload