0

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();
  • 3
    Web browsers can handle chunked transfers transparently. Something else may be wrong here. It's very unlikely that chunked transfer is the problem. – slebetman Aug 05 '16 at 04:55
  • What's the return value of c++_to_javascript look like? Also, why is javascript_to_c++ the endpoint of your Ajax call? – Robert Moskal Aug 05 '16 at 04:56
  • 1
    That code is not waiting for the ajax call to complete but is parsing BEFORE the ajax call completes. If he were to call with regular ajax he would get 100% of the message but he would have to wait minutes (until the server closes connection) – slebetman Aug 05 '16 at 05:19
  • 1
    Can you be more specific than saying it "failed?" What exactly happens? Error message? If so, what. Unexpected result? If so, what? – Jacob Aug 05 '16 at 05:31
  • 1
    As I've mentioned before, NONE of this is necessary. Browsers can handle transfer encoding perfectly fine. Something else is going on. Transfer encoding is not the problem. Try getting the page with `curl` and post the result. Use the `-v` flag. – slebetman Aug 05 '16 at 06:14

1 Answers1

0

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);
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • 1
    `Transfer-Encoding` is unrelated to the content type. It is the technique of transfering; you can have any content type you want with chunked transfer encoding. – Jacob Aug 05 '16 at 05:40
  • 1
    Check your code; your syntax doesn't match what I posted. – Jacob Aug 05 '16 at 05:56
  • 1
    Post your whole code; it's impossible to know what you're actually doing. – Jacob Aug 05 '16 at 06:04
  • 1
    I really don't think there's a browser bug; update your question with your latest attempt, client and server. Ajax will not freeze your browser. If the response is taking 2-3 minutes, then the server is sending the response incorrectly. – Jacob Aug 05 '16 at 06:07
  • Sir its working now. Your last header settings has resolved the issue ( http://stackoverflow.com/a/18311469/285594 ) –  Aug 05 '16 at 06:30