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

  • SEARCH
  • Home
  • 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 9194707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:28:37+00:00 2026-06-17T21:28:37+00:00

I’m fairly new to Node JS but have been experimenting with some AJAX using

  • 0

I’m fairly new to Node JS but have been experimenting with some AJAX using it and also trying out reading and writing files. What I’d like to know now is whether a Node JS server can tell an HTML page to change or if there must be some request from the page in order to receive information from the server about how to change.

To (hopefully) clarify: suppose I have a button with the id ajax-retrieve which when clicked asks for some information from my Node JS server:

$('#ajax-retrieve').click(function () {
    $.ajax({
        url: 'http://localhost:8124/',
        data: 'fn=open',
        dataType: "jsonp",
        cache: false,
        timeout: 5000,
        success: function(data) {
            console.log(data);
            $("#input-ready").html(data.save.text);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('error ' + textStatus + " " + errorThrown);
        }
    });
});

On successfully receiving some data back from the server, the callback then (as you can see) logs the data in the console and puts part of it inside an element with the id input-ready. But if the request fails or times out then an alert box appears giving details of the error.

This is roughly what the server-side code looks like:

http.createServer(function (req, res) {
    "use strict";
    var queryObj = url.parse(req.url, true).query;
    queryObj.fn = queryObj.fn || '';
    queryObj.callback = queryObj.callback || '';

    if (queryObj.callback) {
        if (queryObj.fn === 'open') {
            fs.readFile('jsonp-storage-2.txt', function (err, data) {
                if (err) {
                    throw err;
                } else {
                    queryObj.save = data;
                    res.writeHead(200, {'Content-Type': 'application/javascript'});
                    res.write(queryObj.callback + '({ fn: "' + queryObj.fn + '", save: ' + queryObj.save + ' })');
                    res.end();
                }
            });
        } else {
            console.log('Function (fn property) not recognised.');
        }
    }
}).listen(8124);

But what if the server needs to do something more complicated with the data before it passes it to my HTML page and what if my server’s code may take some time doing that thing? Is there a way for it to chunk the returned data up and return relevant parts when it’s ready or would that require separate requests from my HTML page?

Ideally, I don’t want my HTML page to have to keep requesting the server for information it has already requested to see if it’s ready yet. What I would like is some kind of callback so that when the server is done the HTML page receives the data it needs and changes the display accordingly? This may be a basic question but is what I’ve described possible?

  • 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-17T21:28:38+00:00Added an answer on June 17, 2026 at 9:28 pm

    You seem to be describing long polling, a technique where http connections are held open for a long time with data sent back in chunks as it is available. More generally, I think that you are really interested in server push – servers sending back data without an explicit request by the client, which long polling approximates. My favorite solution for node.js is socket.io, a library that provides a clean wrapper around server push and automatically uses a number of technologies (including long polling) based on what is available in the client browser.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have been unable to fix a problem with Java Unicode and encoding. The
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put

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.