I cannot seem to understand why I cannot access an array I get send back by PHP.
I send this back when AJAX makes a call: $response['success'] = true;
I do this by echo json_encode($response); in my PHP file.
Now I want to access it in Javascript, but response.success doesnt work, it logs 'undefined' in the console. Now I check the response, and that is this: {"success":false}
But if I check for if(response.success) it always sends back false, because response.success is undefined. Does anyone know what I mean and what is causing this issue?
This is my AJAX call:
$$.ajax({
type: 'POST',
url: url + "applogin.php",
crossDomain: true,
data: {
username: e,
password: p
},
//dataType: 'json',
success: function(response) {
console.log(response);
if (response["success"]) {
window.localStorage["username"] = e;
window.localStorage["password"] = md5(p);
mainView.router.loadPage('beurslist.html');
} else {
console.log("Your login failed");
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error- Status: " + textStatus + " jqXHR Status: " + jqXHR.status + " jqXHR Response Text:" + jqXHR.responseText)
},
});
response.successshould be the way. Are your sure you are returning the correct JSON? What doesconsole.log(response)output?dataTypeoption tojson. Otherwise the response will be treated ashtml(by default)dataType(default: Intelligent Guess (xml, json, script, or html))