JSON parse data in javascript from php

Posted by Stefania on Stack Overflow See other posts from Stack Overflow or by Stefania
Published on 2012-11-27T10:15:41Z Indexed on 2012/11/27 11:04 UTC
Read the original article Hit count: 255

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about JavaScript