Reputation: 85855
I have a result like this
{
"Errors" : {
"Failure" : {
"ShowAsPopup" : true,
"ErrorMessage" :"Some Message.",
"PopupTitle" : null
}
},
"IsValid" : false,
"WarningMessage" : null,
"SuccessMessage" : null
}
now if I do Errors.Failure.ShowAsPopup
I get a value. What is expected. However I want to use index instead(if that is possible)
I tried
Errors[0].Failure.ShowAsPopup
but this just gives me undefined. Ideal I would like to have it like Errors[0].[0].ShowAsPopup where I don't have to specify the "Failure" but I might have to rethink that part.
I want a generic way to handle my errors. See some errors require a popup and some are just validation errors. So right now I have them all hardcoded and I am trying to get away from that. I rather just check if that error requires a popup or not.
So instead of
if(response.Errors.Failure) { // alert('hi')};
else if(response.Errors.Failure2 {// alert('hi2')}
and so on I would just have one if statement that could do the check.
Upvotes: 2
Views: 135
Reputation: 11044
// check if any errors at all
if (response.Errors instanceof Object && (response.Errors !== {}))
{
$.each(response.Errors, function(index, error){
// for your example, index will be "Failure", error will be the Object inside
if (error.ShowAsPopup) {
// do your popup thing
}
else
{
// do whatever else
}
});
}
Upvotes: 1
Reputation: 767
You could create an array of JSON objects.
var errors = [
{"Failure": {
"ShowAsPopup":true,
"ErrorMessage":"Some Message.",
"PopupTitle":"Error Message #1"
},
"IsValid":false,
"WarningMessage":null,
"SuccessMessage":null
},
{"Failure": {
"ShowAsPopup":true,
"ErrorMessage":"Some Other Message.",
"PopupTitle":"Error Message #2"
},
"IsValid":false,
"WarningMessage":null,
"SuccessMessage":null
},
]
And then access each error like this:
errors[0].Failure.ErrorMessage
//This returns "Some Message"
errors[1].Failure.ErrorMessage
//returns "Some Other Message"
Upvotes: 0
Reputation: 707996
Your code and data have to match. If the data is this:
{"Errors":{"Failure":{"ShowAsPopup":true,"ErrorMessage":"Some Message.","PopupTitle":null}},"IsValid":false,"WarningMessage":null,"SuccessMessage":null}
which in multi-line form looks like this:
var data = {
"Errors": {
"Failure": {
"ShowAsPopup":true,
"ErrorMessage":"Some Message.",
"PopupTitle":null
}
},
"IsValid":false,
"WarningMessage":null,
"SuccessMessage":null
}
Then, you have to use code to match your data which in this case is Errors.Failure.ShowAsPopup
. Since the data is in object form, you have to use object syntax to read it. Arrays and objects are not interchangable. If you want to use Array syntax to read the data, then the data would need to be a different forum.
You could write a generic error function like this:
if (!data.IsValid) {
handleError(data);
}
function handleError(info) {
if (info.Errors ) {
if (info.Errors.Failure) {
if (info.Errors.Failure.ShowAsPopup) {
// show a popup error here using info.Errors.Failure.ErrorMessage
}
} else if (/* other types of errors here */) {
// handle other types of errors here
}
}
}
Upvotes: 3
Reputation: 1391
What you should be doing is either one of the following. First, you could just simply loop through the errors object like this:
for (var errorName in response.Errors) {
var error = response.errors[errorName];
alert(errorName.ErrorMessage);
}
What is happening here is each property of the response.Errors object is being looped through.
The other option would be to return your errors as an array:
{ "Errors": [{"ShowAsPopup": true, "ErrorMessage": "Some message"}] }
Either way would work nicely.
Upvotes: 0
Reputation: 5074
Errors.Failure.ShowAsPopup = Errors[0][0] = errors[0].ShowAsPopup
Upvotes: 0
Reputation: 2202
curly braces {} signify a structure not an array. So I think this should work:
response["Errors"]["Failure"]["ShowAsPopup"]
Upvotes: 0