YumYumYum YumYumYum - 4 months ago 23
Node.js Question

NodeJS - how to tell the nodeJS server to send not as chunked transfer? so that Ajax can receive it

I have a data coming to NodeJS and then it needs to be served to Ajax query. but because the NodeJS is transfering it as chunked data, none of the general Javascript/JQuery ajax methods are able to capture the packet.

How can i get the values

nodeJS:

http.createServer(options, (req, res) => {
msg = c++_to_javascript();

res.writeHead(200);
res.end(msg);

}).listen(89);


Javascript/JQuery:

received the data as
transfer encoded: chunked
as a result no methods are able to decode the received packets in JS.

enter image description here

How could i solve it from NodeJS or in JS?

TRY 1: FAILED

$.stream(javascript_to_c++,{
open:function(){
console.log("opened");
},
message:function(event){
console.log(event.data);
buf1 = buf1 + event.data;
},
error:function(){
console.log("error");
},
close:function(){
console.log("closed");
}
});


TRY2 : FAILED

var test = $.ajax({
type: "GET",
url: javascript_to_c++,
async: false,
success:function(m) {
buf1 = buf1 + m;
}
}).responseText;
console.log(test);


TRY 3: FAILED

// Define a method to parse the partial response chunk by chunk
var last_index = 0;
function parse(xhr) {
var curr_index = xhr.responseText.length;
if (last_index == curr_index) return; // No new data
var s = xhr.responseText.substring(last_index, curr_index);
last_index = curr_index;
console.log(s);
}

function test() {

var xhr = new XMLHttpRequest();

var url = javascript_to_c++;

xhr.open("GET", url, true);
xhr.send();

// Check for new content every 5 seconds
var interval = setInterval(parse, 5000);

// Abort after 25 seconds
setTimeout(function(){
clearInterval(interval);
parse(xhr);
xhr.abort();
}, 25000);

}

test();

Answer

You just have to properly wait for the response to come back. I'm not too familiar with the other ajax methods you're using but let's look at this for example:

  var test =  $.ajax({
    type: "GET",
    url: javascript_to_c++,
    async: false,
    success:function(m) {
      buf1 = buf1 + m;
    }
  }).responseText;
  console.log(test);

Are you expecting test to be the response? $.ajax returns a Promise representing the asynchronous request. It's not until the success callback is called or the promise is resolved when you'll get the response text. Try this instead:

var test =  $.ajax({
  type: "GET",
  url: javascript_to_c++, // I'm assuming this is a valid URL?
  success:function(content) {
    console.log(content); // This is your actual full response. 
  }
});

As far as having the node process specify the content type, try the content-type header:

res.set('content-type', 'text/plain');
res.end(msg);

Update:

As already pointed out, it's highly doubtful that the Transfer-Encoding thing is actually a problem. It you do want to disable it, do:

res.set('transfer-encoding', '');

...but really, it shouldn't be necessary.

Another Update:

Not using Express? You'll just need to slightly modify this:

res.writeHead(200, { 
  'content-type': 'text/plain',
  'transfer-encoding': '' // not recommended... 
});
res.end(msg);
Comments