JSON从php解析javascript中的数据

I'm trying to retrieve data in a javascript file from a php file using json.

$items = array(); 
while($r = mysql_fetch_array($result)) { 
    $rows = array( 
        "id_locale" => $r['id_locale'], 
        "latitudine" => $r['lat'], 
        "longitudine" => $r['lng'] 
    ); 
    array_push($items, array("item" => $rows)); 
} 
ECHO json_encode($items);

and in the javascript file I try to recover the data using an ajax call:

$.ajax({
    type:"POST",
    url:"Locali.php",
    success:function(data){
        alert("1");
        //var obj = jQuery.parseJSON(idata);
        var json = JSON.parse(data);
        alert("2");
        for (var i=0; i<json.length; i++) {
            point = new google.maps.LatLng(json[i].item.latitudine,json[i].item.longitudine);
            alert(point);
        }
    }
})

The first alert is printed, the latter does not, it gives me error: Unexpected token <.... but I do not understand what it is.

Anyone have any idea where am I wrong?

I also tried to recover the data with jquery but with no positive results.

This should do it.

$.post("Locali.php",{
    // any parameters you want to pass
},function(d){
    alert("1");
    for (var i=0; i<d.length; i++) {
      point = new google.maps.LatLng(d[i].item.latitudine,d[i].item.longitudine);
      alert(point);
    }
}, 'json');

the PHP is fine if it is giving the response you mentioned above.

data $.ajax({ type:"POST", dataType: json, url:"Locali.php", success:function(data){ for (i in data) { point = new google.maps.LatLng(json[i].item.latitudine,json[i].item.longitudine); alert(point);
} } })

Try it like that.

You should be Ok with this slight modifications:

$items = array(); 
while($r = mysql_fetch_array($result)) { 
    $items[] = array( 
        "id_locale" => $r['id_locale'], 
        "latitudine" => $r['lat'], 
        "longitudine" => $r['lng'] 
    ); 
} 
echo json_encode($items);

And the jQuery:

$.ajax({
    type:"POST",
    dataType: 'json',
    url:"Locali.php",
    success:function(data){
        console.log(data);
        for (var i=0; i<data.length; i++) {
            point = new google.maps.LatLng(data[i].item.latitudine,data[i].item.longitudine);
            alert(point);
        }
    }
})

yeah just try

for (var i=0; i<json[0].length; i++) {

cause you have an object there..

I think u should look more for the data you are getting from the php file. Definitely this is a parse error and must be missing some bracket/something else, ultimately not making the data returned to be a json parseable string.