Reference for background
A little confused on this stream readable syntax
The current situation is I am using res.write in express to write out the json object that is slowly being streamed by using JSON.Stringify.
app.get('/:foo', function (req, res) {
var connection = new sql.Connection(config, function(err) {
var request = new sql.Request(connection);
request.query(---SQL QUERY---);
res.header("Content-Type", "application/json; charset=utf-8");
res.header("Cache-Control", "max-age=3600");
res.write("[");
request.on('row', function (data) {
res.write(JSON.stringify(data)+",");
)};
request.on('done', function () {
res.write("]");
res.end();
});
That is roughly the syntax I am using,
the Headers come through fine,
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Cache-Control: max-age=3600
Date: Fri, 22 Aug 2014 13:08:34 GMT
Connection: keep-alive
Transfer-Encoding: chunked
This call was done with an AJAX call and an Oboe call. The problem is even though this JSON shows up in my network as status 200, I cannot use it at all. Oboe emits a fail, and AJAX emits a fail as well.
A friend said it could be something to do with DOM timing but couldnt really explain it, he just said he knows sometimes his devs would encase the call in a <div> so it renders inside the dom and stays? I dont really understand what he means.
Should I be using something like jQuery Stream?
Previously when I had my JSON in a callback it was loading fine with the AJAX calls but writing the objects in a stream to res seems to cause it to break?
Figured it out,
my JSON was adding a trailing comma.
While the browser does not care about a trailing comma and will read the object just fine, AJAX Call and Oboe.js Call simply fails with unexpected ]