Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send data from JQuery AJAX request to Node.js server
    primarykey
    data
    text
    <p><strong>What i want to do:</strong><br> Simply send some data (json for example), to a node.js http server, using jquery ajax requests.</p> <p>For some reason, i can't manage to get the data on the server, cause it never fires the 'data' event of the request.</p> <p><strong>Client code:</strong></p> <pre><code>$.ajax({ url: server, dataType: "jsonp", data: '{"data": "TEST"}', jsonpCallback: 'callback', success: function (data) { var ret = jQuery.parseJSON(data); $('#lblResponse').html(ret.msg); }, error: function (xhr, status, error) { console.log('Error: ' + error.message); $('#lblResponse').html('Error connecting to the server.'); } }); </code></pre> <p><strong>Server code:</strong></p> <pre><code>var http = require('http'); http.createServer(function (req, res) { console.log('Request received'); res.writeHead(200, { 'Content-Type': 'text/plain' }); req.on('data', function (chunk) { console.log('GOT DATA!'); }); res.end('callback(\'{\"msg\": \"OK\"}\')'); }).listen(8080, '192.168.0.143'); console.log('Server running at http://192.168.0.143:8080/'); </code></pre> <p>As i said, it never gets into the 'data' event of the request.</p> <p><strong>Comments:</strong><br> 1. It logs the 'Request received' message;<br> 2. The response is fine, im able to handle it back on the client, with data;</p> <p>Any help? Am i missing something?</p> <p>Thank you all in advance.</p> <p><strong>EDIT:</strong><br> Commented final version of the code, based on the answer:</p> <p><strong>Client code:</strong> <br></p> <pre><code>$.ajax({ type: 'POST' // added, url: server, data: '{"data": "TEST"}', //dataType: 'jsonp' - removed //jsonpCallback: 'callback' - removed success: function (data) { var ret = jQuery.parseJSON(data); $('#lblResponse').html(ret.msg); }, error: function (xhr, status, error) { console.log('Error: ' + error.message); $('#lblResponse').html('Error connecting to the server.'); } }); </code></pre> <p><br> <strong>Server code:</strong><br></p> <pre><code>var http = require('http'); http.createServer(function (req, res) { console.log('Request received'); res.writeHead(200, { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*' // implementation of CORS }); req.on('data', function (chunk) { console.log('GOT DATA!'); }); res.end('{"msg": "OK"}'); // removed the 'callback' stuff }).listen(8080, '192.168.0.143'); console.log('Server running at http://192.168.0.143:8080/'); </code></pre> <p><br> Since i want to allow Cross-Domain requests, i added an implementation of <a href="http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing">CORS</a>.</p> <p>Thanks!</p>
    singulars
    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.
 

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