Problem
I'm trying to log the data returned from a JSON call, and console.log(jsonData)
doesn't seem to be working.
Code
$(document).ready(function() {
$("#users li a:first-child").click(function() {
var id = this.href.replace(/.*=/, "");
this.id = "delete_link_" + id;
if(confirm("Are you sure you want to delete this user?"))
{
$.getJSON("delete.php?ajax=true&id=" + id, function(data) {
console.log(data.success);
});
}
return false;
});
});
Returned by delete.php
{"id": $id, "success": 1}
upon success.{"id": $id, "success": 0, "error": "Could not delete user."}
upon failure.
Your JSON isn't valid:
{'id' : 2, 'success' : 1}
It should have double quotes:
{"id" : 2, "success" : 1}
Based on that I think you're manually building the JSON string, I would suggest using json_encode()
instead:
$result = new stdClass;
$result->id = 2;
$result->success = 1;
echo json_encode($result);
Outputs
{"id":2,"success":1}