I have a jQuery and a PHP file. If in PHP the process was done correctly, it returns 2 (by echo json_encode(2)). I catch it in the jQuery file, and I show a string on a HTM div. I can do This part without any problem.
The thing that is being difficult to me is to return the erorr to ajax, when it occurs, to show on the HTML div. I thought on return a JSON array {"error": "xxx"} where "xxx" was the error generated by php.
So the problem is that I can't get the value of the "error" key to show it. I read a lot of similar topics on the website, but they are about reading values of more than one object.
jQuery file:
$('#log').submit(function(event) {
event.preventDefault();
var stuff = $(this).serializeArray();
stuff.push({name:'tag', value:'login'});
$.ajax({
url: './php/logreg.php', //Como si fuera el action del form
type: 'post', //Por defecto es get
dataType: 'json',
data: stuff,
beforeSend: function(){
$('#span-login-icon').css('display','inline');
}
})
.done(function(data){
if(data == 2){
$('#span-login-resp').html('Correcto');
}
else{
$('#span-login-resp').html(data);
}
});
});
PHP file:
if($object->queError() === ''){
echo json_encode(2);
}
else{
echo json_encode('{"error"'.':"'.$object->queError().'"'.'}'); //output: {"error":"xxx..."}
}
if (data.hasOwnProperty('error')) { /* ... */ } else { /* ... */ }