Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed to send data back from a NodeJS server to a JQuery client
    text
    copied!<p>I'm trying to send some files and data back from a NodeJS server to a client that works in JQuery. I use res.sendfile() to return files from the NodeJS server, but that downloads the file on the client computer. I only want to send the file back to the JQuery code (Written in Typescript) and enable it being manipulated or rendered by the JQuery code. How can I do that?</p> <p>I am also trying to send JSON data back to the client but the JSON data gets rendered on the client side, instead of being handled by the JQuery code.</p> <p>Here's the client side code that receives a JSON message after file upload</p> <pre><code>function sendFile() { $(new ID().setAsRoot().selectId()).append( "&lt;form id=\"fileUploadForm\" accept-charset=\"utf-8\" method=\"post\" action=\"/upload\" enctype=\"multipart/form-data\"&gt;&lt;input id = \"filename\" type=\"file\" name=\"userfile\" multiple=\"multiple\" /&gt;&lt;button type=\"submit\"&gt; Upload &lt;/button&gt;&lt;/form&gt;"); var $form = $("#fileUploadForm"); alert("Started"); $form.submit(function (e) { // perform client side validations if any this.ajaxSubmit({ error: function () { alert("Error"); }, success: function (response) { var x = response.message; alert("Success : " + x); } }); // Important: stop event propagation return false; }); } </code></pre> <p>Here's the server side code for sending the response:</p> <pre><code>respond: function respond(req, res, next) { console.log("Responding"); var response = { result: 'success', upload: req.uploadLink, message: 'File uploaded!' }; res.status(200).json(response); console.log("Response complete"); }, </code></pre> <p>Here's the JSON getting rendered on the screen:</p> <pre><code>{ "result": "success", "upload": "\\data\\tempFile4", "message": "File uploaded!" } </code></pre>
 

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