【发布时间】:2016-10-12 01:04:43
【问题描述】:
我基于以下解决方案在基于 Angular 的客户端和 node.js 后端实现了下载文件功能:https://stackoverflow.com/a/20904398/1503142。一般来说,这是可行的,但有时我会收到“SyntaxError:JSON 中位置 x 的意外数字”以及“TypeError:无法读取未定义的属性‘消息’”。
一些观察:
- 节点服务器端似乎一切正常,因为我总是在客户端得到响应。有问题的错误由客户端报告;服务器未报告任何错误。
- 日志文件由时间戳和基本日志信息文本组成。文本可以包含 ASCII 字符
- 使用 Postman,响应每次都有效,这使人们认为可能是响应有问题的是 http$ 代码。 Postman的响应Header信息表明响应是Content-Type->text/plain; charset=utf-8。
- 我正在使用 AngularJS v1.2.21 和 Node v0.12.13
这是客户端代码:
$http({
method: 'GET',
url: "/api/logging/logfiles/" + logFile,
headers: { 'Content-Type': 'text/plain;charset=utf-8' }
}).
success(function (data, status, headers, config) {
var anchor = angular.element('<a/>');
anchor.attr({
href: 'data:text/plain;charset=utf-8,' + encodeURIComponent(data),
target: '_blank',
download: logFile
})[0].click();
}).
error(function (data, status, headers, config) {
console.log('Hence my visit to StackOverflow!')
});
这是服务器端代码:
app.get('/api/logging/logfiles/:logfile', function (req, res, next) {
logDirectory = './log';
fs.readFile(logDirectory + "/" + req.params.logfile, 'utf8', function (err, data) {
if (err) {
res.send("Something broke!");
}
else {
res.set({ 'Content-Type': 'text/plain; charset=utf-8' });
res.send(data);
}
});
});
我怀疑这与日志文件的内容有关。既然我指定了 text/plain 内容,为什么会出现 JSON 解析错误?
【问题讨论】:
-
需要在请求中设置dataType而不是header。请求标头没有意义,因为 GET 请求没有内容类型
-
你能检查一下 mimetype 吗? mime.lookup(logDirectory + "/" + req.params.logfile)
标签: javascript angularjs node.js http get