Q: Printing json from php in a div I am trying to retrieve json from a php file and print the names in a div. I am unable to get any result. 
HTML: 
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('button').click(function(){
        $.ajax({
            type: "post",
            url: "services/myphp1.php",
            dataType: "json",
            async: false,
            contentType: 'application/json',
            success: function(data) {
                var html = "";
                JSON.parse(data).forEach(function(item){ 
                    html+ = item.name;
                });
                $('#div1').append(html);
            }
        });
    });
});
</script>
</head>
<body>
<button>Click Me!</button>
<div id="div1"></div>
</body>
</html>

PHP: 
<?php
$alpha = '{Persons:[{name:"Robert",age:"33"},{name:"Lina",age:"23"},{name:"Emerald",age:"19"}]}';
echo json_encode($alpha);
?>

Can someone tell me where am I making a mistake? I want to get the names of 3 people in my div named '#div1'. Thanks in advance!!! ;( .... 

A: Your JSON structure is incorrect. To make sure its well structured, let PHP do the work for you. In PHP, build your JSON like this:
$arr = array(
    "Persons" => array(
        array(
            "name" => "Robert",
            "age" => 33
        ),
        array(
            "name" => "Lina",
            "age" => 23
        ),
        array(
            "name" => "Emerald",
            "age" => 23
        )
    )
);
header("Content:application/json");
echo json_encode($arr);

Then in JS, you will be receiving pure JSON, so no parsing is needed, you would just need to iterate. I would also use $.getJSON:
$.getJSON("services/myphp1.php", function (data) {
    data.Persons.forEach(function (i) {
        $('#div1').append(i.name);
    });
});


A: Amongst other things, assuming your php is correctly returning that JSON, your returned JSON is an object with the property 'Persons', that is an array.
So try:
JSON.parse(data).Persons.forEach( ... )


A: I think code has error on line
html+ = item.name; 

it must 
html += item.name;
Your json is not valid.
check here:
http://jsonlint.com/
