Here is my error callback function from an ajax call.
这是来自ajax调用的错误回调函数。
error: function(xhr, status, error) {
var responseObj = jQuery.parseJSON( xhr.responseText );
}
From that I am sending this to the console:
我把这个发给控制台:
console.log(responseObj.message);
Which is returning this:
这是返回:
Object {Invalid or missing parameters: Object}
Invalid or missing parameters: Object
email_already_in_use: "'Email' already in use"
If I stringify the response like this:
如果我像这样压缩响应:
var responseMsg = responseObj.message;
if(typeof responseMsg =='object') {
var respObj = JSON.stringify(responseMsg);
console.log(respObj);
}
I get this:
我得到了这个:
{"Invalid or missing parameters":{"email_already_in_use":"'Email' already in use"}}
How can I print to the user that their email is already in use?
我如何向用户打印他们的电子邮件已经在使用?
complete callback function:
完整的回调函数:
error: function(xhr, status, error) {
var responseObj = jQuery.parseJSON( xhr.responseText );
var responseMsg = responseObj.message;
if(typeof responseMsg =='object') {
var respObj = JSON.stringify(responseMsg);
console.log(respObj);
} else {
if(responseMsg ===false) {
console.log('response false');
} else {
console.log('response something else');
}
}
console.log(responseObj.message);
}
1 个解决方案
#1
3
You could do something like this:
你可以这样做:
var errorMessages = responseObj.message["Invalid or missing parameters"];
for (var key in errorMessages) {
if(errorMessages.hasOwnProperty(key)){
console.log(errorMessages[key]);
}
}
If you have different kinds of messages (not only "Invalid or missing parameters"), you should iterate the message array first:
如果您有不同类型的消息(不仅是“无效或缺失的参数”),您应该首先迭代消息数组:
var errorMessages = responseObj.message;
for (var errorType in errorMessages){
if(errorMessages.hasOwnProperty(errorType)){
console.log(errorType + ":");
var specificErrorMsgs = errorMessages[errorType];
for (var message in specificErrorMsgs) {
if(specificErrorMsgs.hasOwnProperty(message)){
console.log(specificErrorMsgs[message]);
}
}
}
}
#1
3
You could do something like this:
你可以这样做:
var errorMessages = responseObj.message["Invalid or missing parameters"];
for (var key in errorMessages) {
if(errorMessages.hasOwnProperty(key)){
console.log(errorMessages[key]);
}
}
If you have different kinds of messages (not only "Invalid or missing parameters"), you should iterate the message array first:
如果您有不同类型的消息(不仅是“无效或缺失的参数”),您应该首先迭代消息数组:
var errorMessages = responseObj.message;
for (var errorType in errorMessages){
if(errorMessages.hasOwnProperty(errorType)){
console.log(errorType + ":");
var specificErrorMsgs = errorMessages[errorType];
for (var message in specificErrorMsgs) {
if(specificErrorMsgs.hasOwnProperty(message)){
console.log(specificErrorMsgs[message]);
}
}
}
}