jac*_*cob 5 xmlhttprequest mongodb node.js express angularjs
我正在使用本机mongo rest api对应用程序进行原型设计,其中Node返回大约400K的json.我使用以下内容来请求mongo的原生api并返回结果:
http.request(options, function(req)
{
req.on('data', function(data)
{
console.log(data,data.rows);
response.send( 200, data );
}
);
}
)
.on('error', function(error)
{
console.log('error\t',error);
response.send(500, error);
}
)
.end();
Run Code Online (Sandbox Code Playgroud)
当我http://localhost:8001/api/testdata通过curl点击时,响应是正确的(从console.logcurl接收到的节点控制台输出的内容).但是当我在我的应用程序中通过ajax点击它时,流被...中断,甚至data输出到Node的控制台(终端)是奇数:它有多个EOF,并且Chrome的开发工具中的调用的网络>响应在第一个EOF结束.
另一个奇怪的事情:data看起来像:
{
"offset": 0,
"rows": [ … ]
}
Run Code Online (Sandbox Code Playgroud)
但在Node和客户端(角度)都不能引用data.rows(它返回undefined).typeof data回报[object Object].
编辑 curl和angular的请求标头(由Node报告)是:
req.headers: {
'x-action': '',
'x-ns': 'test.headends',
'content-type': 'text/plain;charset=utf-8',
connection: 'close',
'content-length': '419585'
}
Run Code Online (Sandbox Code Playgroud)
编辑我直接检查了角度和卷曲中的响应头(而不是来自Node),发现存在分歧(来自curl和angular的相同输出直接而不是来自节点):
access-control-allow-headers: "Origin, X-Requested-With, Content-Type, Accept"
access-control-allow-methods: "OPTIONS,GET,POST,PUT,DELETE"
access-control-allow-origin: "*"
connection: "keep-alive"
content-length: "65401" // <---------------- too small!
content-type: "application/octet-stream"
// ^-- if i force "application/json"
// with response.json() instead of response.send() in Node,
// the client displays octets (and it takes 8s instead of 0s)
date: "Mon, 15 Jul 2013 18:36:50 GMT"
etag: ""-207110537""
x-powered-by: "Express"
Run Code Online (Sandbox Code Playgroud)
jac*_*cob 10
Node的http.request()以块的形式返回数据以进行流式传输(如果他们明确说明这一点会很好).因此,有必要将每个块写入Express的响应主体,监听http请求的结束(没有真正记录),然后调用response.end()实际完成响应.
var req = http.request(options, function(res)
{
res.on( 'data', function(chunk) { response.write(chunk); } );
res.on( 'end', function() { response.end(); } );
}
);
req.on('error', function(error) { … });
req.end();
Run Code Online (Sandbox Code Playgroud)
其中response是快速的响应的初始客户端请求(卷曲的或有角的AJAX调用).
| 归档时间: |
|
| 查看次数: |
7759 次 |
| 最近记录: |