Wasn’t sure how to phrase the title but here is my problem:
Here is my javascript:
$.post("register", element.serialize(),
function(data){
if(data.success == "true"){
// Do whatever
} else if (data.success == "false"){
for (i = 0; i < data.errors.length; i++){
// This is what I need to change
alert(data.errors[i][0]);
}
}
}, "json");
Here is the incoming JSON string:
{"success":"false","errors":{"username":"error with username","email":"error with email"}}
Broken up, that is:
errors: {
email: "error with email"
username: "error with username" }
success: "false"
I want the alerts to say “error with email” and “error with username”
Nothing I seem to be doing is working though, any ideas?
Thanks
data.errorsis not an array. It’s an object with properties:It’s unclear from your question what you want to do with those two properties, but that’s how you access them.
If you want to iterate the properties of the errors object, you can do that like this: