3

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.

7
  • 1
    Don't use JSON.parse in your success function. Give your $.ajax attribute dataType: 'json' and then console.log the JSON response to see whether you got a null or an object with properties. If you got a null, something's wrong with the data in your PHP script. Commented Nov 27, 2012 at 10:19
  • Also try putting exit(); after json_encode() function Commented Nov 27, 2012 at 10:20
  • my JSON data is: [{"item":{"id_locale":"50","latitudine":"44.4794995","longitudine":"11.364192099999968"}}] in JSON lint the json file is ok Commented Nov 27, 2012 at 10:21
  • adding exit(), dont' work, now I try with the console.log Commented Nov 27, 2012 at 10:23
  • If You specify dataType: 'json' in Your jQuery AJAX call it should be enough in Your PHP script to do return $items; instead of echo json_encode($items);. Commented Nov 27, 2012 at 10:23

5 Answers 5

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

how can I take the parameters?
you were not passing any parameters in your question, but I left the area open because I almost always pass something. they would be in array format. simpler; where the comment is you could pass comma delineated data like name:value, name2:value2
0

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.

Comments

0

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);
        }
    }
})

12 Comments

Didn't... If You specify the dataType:'json' it is not required to json_encode the PHP response...This is how I use it in many projects...
I have never heard of that before. maybe I will try it sometime. I would think that the PHP response would just be Array()
The difference is I am using return $items; instead of echoing json string... So any array or object get passed directly to AJAX call as response...
so you are saying that PHP automatically encodes arrays into JSON format. I did not know that. It serializes the array no matter what type of characters are used?
@NappingRabbit Sorry for the mistyfication - I looked at out framework and the base controller class. It checks whether the request comes from AJAX (the call contains parameter async=1) and in that case it takes the response from the action called (in my case the return) and echoes it json_encoded... Sorry, my bad.
|
0

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.

1 Comment

variable json is not defined (when making variable point), return variable in what you wrote is data
-1

yeah just try

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

cause you have an object there..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.