【问题标题】:Angular-Oboe + NodeJS stream error non-whitespace character beforeAngular-Oboe + NodeJS 流错误非空白字符之前
【发布时间】:2015-10-17 20:54:12
【问题描述】:

NodeJS 服务器:

var exec = require('child_process').exec;

app.get('/ls', function(req, res) {
  exec('ls',
    function (error, stdout, stderr) {
      res.send(stdout);
  });
});

Angular 客户端(来自its GitHubPage 示例的标准实现):

$scope.myData = [];


Oboe({
        url: $scope.baseUrl + ":" + $scope.port + "/ls",
        pattern: '*',
        start: function(stream) {
            // handle to the stream
            $scope.stream = stream;
            $scope.streamStatus = 'started';
        },
        headers: {
            Authorization: 'Basic '  + $rootScope.globals.currentUser['authdata']
        },
        done: function() {
            $scope.streamStatus = 'done';
        }
    }).then(function() {
        // finished loading
    }, function(error) {
      console.log(error);
        // handle errors
    }, function(node) {
        // node received
        console.log(node);
        $scope.myData.push(node);
    });

当我检查请求时,我得到以下成功响应:

data
index.js
license
node_modules
npm-debug.log
package.json
readme.md
routes
test.js

但双簧管似乎不想处理这个问题。这是我在客户端遇到的错误:

Object {statusCode: undefined, body: undefined, jsonBody: undefined, thrown: Error: Non-whitespace before {[.
Ln: 1
Col: 1
Chr: d
    at Error (native)
    at c (http://www.samh…}body: undefinedjsonBody: undefinedstatusCode: undefinedthrown: Error: Non-whitespace before {[.
Ln: 1
Col: 1
Chr: d
    at Error (native)
    at c (http://localhost:8080/vendor/oboe/dist/oboe-browser.min.js:1:1633)
    at h (http://localhost:8080/vendor/oboe/dist/oboe-browser.min.js:1:2054)
    at z (http://localhost:8080/vendor/oboe/dist/oboe-browser.min.js:1:1233)
    at a.emit (http://localhost:8080/vendor/oboe/dist/oboe-browser.min.js:1:7560)
    at XMLHttpRequest.j (http://localhost:8080/vendor/oboe/dist/oboe-browser.min.js:1:6314)__proto__: Object

我也试过 JSON.stringify()-ing 标准输出无济于事。为什么我会收到此错误?提前致谢!

【问题讨论】:

  • 你试过res.status(200).json(stdout)吗?
  • 谢谢!不过,它仍然抛出同样的错误。
  • 似乎双簧管在查看错误时根本没有收到任何响应。如果您将 Oboe 配置对象中的 url 属性更改为:url: '/ls',因为它将默认为当前的 <protocol>://<host>:<port>
  • 在浏览器的网络检查器中,我看到所有数据的 200 OK 响应。所以我认为双簧管正确地调用了正确的 URL。
  • 你能创建一个服务器返回的数据的粘贴吗?只是为了确保它实际上是有效的 JSON。您还可以尝试将您的响应包装在一个对象中,如下所示:res.status(200).json({ data: stdout })

标签: javascript angularjs node.js stream stdout


【解决方案1】:

请在响应中发送一个对象

示例:res.status('yourstatuscode').send(passanobjecthere);

res.status(401).send({
message: 'Server error'
})

【讨论】:

    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    相关资源
    最近更新 更多