【发布时间】:2014-08-22 13:14:53
【问题描述】:
背景参考
A little confused on this stream readable syntax
目前的情况是我在 express 中使用 res.write 来写出正在使用 JSON.Stringify 慢慢流式传输的 json 对象。
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();
});
这就是我使用的大致语法,
标题很好,
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
此调用是通过 AJAX 调用和 Oboe call 完成的。问题是即使这个 JSON 在我的网络中显示为状态 200,我也根本无法使用它。双簧管发出失败,AJAX 也发出失败。
一位朋友说这可能与 DOM 计时有关,但无法真正解释,他只是说他知道有时他的开发人员会将调用封装在 <div> 中,以便它在 dom 中呈现并保留?我真的不明白他的意思。
我应该使用像 jQuery Stream 这样的东西吗?
以前,当我在回调中使用 JSON 时,它可以通过 AJAX 调用正常加载,但将流中的对象写入 res 似乎会导致它中断?
【问题讨论】:
标签: jquery ajax node.js express stream