I've searched a lot for this but the answers that I find doesn't work but I think that the problem is mine.
JavaScript:
$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'json',
success: function(response) {
if(response.status == true) {
alert('ok');
} else {
alert('error');
}
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
PHP
if(User::addFavLater($id, $user, 'favs')) {
$result = array("status" => true);
} else {
$result = array("status" => false);
}
header('Content-type: application/json');
echo json_encode($result);
So, this is an excerpt of code that I have and I think can you realising. I want PHP to send a JSON response into JavaScript but this is not happening and JavaScript gives me this error:
parsererror / SyntaxError: Unexpected token <
I'm using the 2.1.1 version of JQuery.
P.S.: Sorry for my english.
In the method $.ajax
, the success callback receives the response of the server (the JSON file) in the variable data
(success:function(data){...}
). Thus, try to recover the value you want from this variable (in your case, something like data.response
rather than just response
).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments