I installed Mamp to work locally on my website. But something strange happened. I have normal return on json (the firebug console display it) but the console log display "undefined" (!)
So firebug display my ajax request and json return :
POST http://local/test.php 200 OK 7ms
{"testjson":"ok"}
But console log display : undefined An idea ?
I checked and json 1.2 is right enabled on Mamp.
test.html :
<script type='text/javascript'>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response)
{
console.log(response['testjson']);
}
});
});
</script>
test.php :
if($_POST['action']=="display")
{
$response['testjson'] = "ok";
header('Content-type: application/json');
echo json_encode($reponse);
exit;
}
Please remove the below line and try:
header('Content-type: application/json');
Or try one of these following variants:
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response) {
console.log(response.testjson);
}
});
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response) {
response = JSON.parse(response);
console.log(response['testjson']);
}
});
$.ajax({
type: "POST",
url: "mod/test.php",
data: "action=display",
success: function(response) {
response = JSON.parse(response);
console.log(response.testjson);
}
});