如何使用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个回答


试试看。
var error = JSON.parse(jqXHR.responseJSON);
console.log(error.message); </ p>
     </ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐