weixin_33696106 2016-02-21 16:13 采纳率: 0%
浏览 122

如何使用JSON.parse

I have the following code in ajax request:

    403: function(jqXHR) {
                var error = jqXHR.responseText;
                console.log(error);
            }

and when this error happens i get next message in console:

HttpError: Wrong password<br> &nbsp; &nbsp;at 
c:\Users\...path...js:18:29<br> &nbsp; &nbsp;at 
c:\Users\...path...\async.js:52:16<br> &nbsp; &nbsp;at 
Immediate._onImmediate 
(c:\Users\...path...
ode_modules\async\lib\async.js:1201:34)<br>
&nbsp; &nbsp;at processImmediate [as _immediateCallback] 
(timers.js:383:17)

When i try to parse this error and get the error.message in such way:

var error = JSON.parse(jqXHR.responseText);
    console.log(error.message);

I have the following message in console:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
.complete()
m.Callbacks/j()
m.Callbacks/k.fireWith()
x()
.send/b()

here is the server-side code:

schema.statics.authorize = function(username, password, callback) {
        var User = this;

        async.waterfall([
            function(callback) {
                User.findOne({username: username}, callback);
            },
            function(user, callback) {
                if (user) {
                    if (user.checkPassword(password)) {
                        callback(null, user);
                    } else {
                        callback(new AuthError("Wrong password"));
                    }
                } else {
                    var user = new User({username: username, password: password});
                    user.save(function(err) {
                        if (err) return callback(err);
                        callback(null, user);
                    });
                }
            }
        ], callback);
    };


    exports.User = mongoose.model('User', schema);




    function AuthError(message) {
        Error.captureStackTrace(this, AuthError);

        this.message = message;
    }

    util.inherits(AuthError, Error);

    AuthError.prototype.name = 'AuthError';

    exports.AuthError = AuthError;

How can i display an error.message to console.log?

  • 写回答

1条回答

  • weixin_33690367 2016-02-21 16:18
    关注

    Try this instead. var error = JSON.parse(jqXHR.responseJSON); console.log(error.message);

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?