Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8868175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:12:26+00:00 2026-06-14T17:12:26+00:00

What i want to do: Simply send some data (json for example), to a

  • 0

What i want to do:
Simply send some data (json for example), to a node.js http server, using jquery ajax requests.

For some reason, i can’t manage to get the data on the server, cause it never fires the ‘data’ event of the request.

Client 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.');
    }
});

Server 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/');

As i said, it never gets into the ‘data’ event of the request.

Comments:
1. It logs the ‘Request received’ message;
2. The response is fine, im able to handle it back on the client, with data;

Any help? Am i missing something?

Thank you all in advance.

EDIT:
Commented final version of the code, based on the answer:

Client 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.');
}
});

Server 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/');

Since i want to allow Cross-Domain requests, i added an implementation of CORS.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:12:27+00:00Added an answer on June 14, 2026 at 5:12 pm

    To get the ‘data’ event to fire on the node.js server side, you have to POST the data. That is, the ‘data’ event only responds to POSTed data. Specifying ‘jsonp’ as the data format forces a GET request, since jsonp is defined in the jquery documentation as:

    “jsonp”: Loads in a JSON block using JSONP. Adds an extra “?callback=?” to the end of your URL to specify the callback

    Here is how you modify the client to get your data event to fire.

    Client:

    <html>
    <head>
        <script language="javascript" type="text/javascript" src="jquery-1.8.3.min.js"></script>
    </head>
    
    <body>
        response here: <p id="lblResponse">fill me in</p>
    
    <script type="text/javascript">
    $(document).ready(function() {
        $.ajax({
            url: 'http://192.168.0.143:8080',
            // dataType: "jsonp",
            data: '{"data": "TEST"}',
            type: 'POST',
            jsonpCallback: 'callback', // this is not relevant to the POST anymore
            success: function (data) {
                var ret = jQuery.parseJSON(data);
                $('#lblResponse').html(ret.msg);
                console.log('Success: ')
            },
            error: function (xhr, status, error) {
                console.log('Error: ' + error.message);
                $('#lblResponse').html('Error connecting to the server.');
            },
        });
    });
    </script>
    
    </body>
    </html>
    

    Some helpful lines to help you debug the server side:

    Server:

    var http = require('http');
    var util = require('util')
    http.createServer(function (req, res) {
    
        console.log('Request received: ');
        util.log(util.inspect(req)) // this line helps you inspect the request so you can see whether the data is in the url (GET) or the req body (POST)
        util.log('Request recieved: \nmethod: ' + req.method + '\nurl: ' + req.url) // this line logs just the method and url
    
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        req.on('data', function (chunk) {
            console.log('GOT DATA!');
        });
        res.end('callback(\'{\"msg\": \"OK\"}\')');
    
    }).listen(8080);
    console.log('Server running on port 8080');
    

    The purpose of the data event on the node side is to build up the body – it fires multiple times per a single http request, once for each chunk of data that it receives. This is the asynchronous nature of node.js – the server does other work in between receiving chunks of data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to simply read some JSON data from a URL, then turn that
I created a client in Java and I want simply to send some data
I want to send some data along with coordinates to a remote server each
I want to send and receive some data from my server, but I don't
I have problem with chrome.extension and jQuery Post. I want post some data (get
I have two C# programs and I want to send some data back and
I'm using the ASIHTTPRequest package to send some data from the iPhone to my
i want to send some data from Action Helper to view Partial and i
I want to send my Application to background simply clicking on Button inside my
I simply want to edit some commits with git rebase -i HEAD~2 inside the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.