I have an issue with parsing json in my success function (ajax). I am now starting to learn json properly, but I simply can't get this to work.
I have this jquery function to submit data:
$("#loginForm").on('submit', function(form)
{
form.preventDefault();
var tdata= $("#loginForm").serializeArray();
var that = $(this),
data = tdata;
$.ajax({
url: that.attr('action'),
type: 'POST',
data: data,
success: function(data){
alert(data["error"]["email"]);
},
error: function(){
alert(data["error"]["email"]);
}
});
});
In the controller I have this:
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|trim|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required');
.....
$arrReturnData['error'] = $this->form_validation->error_array();
echo json_encode($arrReturnData);
In firefox when I trace the requests, the string received is:
{"error":{"email":"The Email field is required.","password":"The Password field is required."}}
But I cannot parse it :( I don't know how, and I've spent more than 3 hours just on this part. Everything works just that I can't parse this.
I tried like:
data.error["email"]
or
data[0]["email"]
But It fails to get the email error message. Please help :)