javascript - Angular http$ response for text/plain file download fails with SyntaxError: Unexpected number in JSON -
i have implemented download file feature on angular-based client , node.js backend based on following solution: https://stackoverflow.com/a/20904398/1503142. in general works, receive "syntaxerror: unexpected number in json @ position x" combined "typeerror: cannot read propery 'messages' of undefined".
a few observations:
- everything appears working on node server-side, because response @ client. error in question reported client; no errors reported server.
- the log file consists of time stamp, basic log information text. text contains ascii characters
- using postman, response works every time, lends idea http$ code might having issue response. postman's response header information indicates response content-type->text/plain; charset=utf-8.
- i using angularjs v1.2.21 , node v0.12.13
here's client-side code:
$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 visit stackoverflow!') });
here's server-side code:
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); } }); });
i suspect related contents of log file. since i've specified text/plain content, why there json parsing error?
try :
app.get('/api/logging/logfiles/:logfile', function (req, res, next) { logdirectory = './log'; res.sendfile(logdirectory + "/" + req.params.logfile); });
Comments
Post a Comment