SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error on jason parse

    Dear All

    I want display the map by taking lat and long values from database using php.

    For that I got this code from one site, but it is not working.

    I have less knowledge of JSON.

    HTML Code:
    <!DOCTYPE html>
     
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Google Maps Example</title>
            <style type="text/css">
                body { font: normal 14px Verdana; }
                h1 { font-size: 24px; }
                h2 { font-size: 18px; }
                #sidebar { float: right; width: 30%; }
                #main { padding-right: 15px; }
                .infoWindow { width: 220px; }
            </style>
            <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
            <script type="text/javascript">
            
                    
            var map;
             
            // Ban Jelacic Square - Center of Zagreb, Croatia
            var center = new google.maps.LatLng(45.812897, 15.97706);
    
             
            function init() 
            {
                 
                var mapOptions = {
                    zoom: 13,
                    center: center,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                }
                 
                map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
                             
            
                        
            makeRequest('locations.php', function(data) 
            {
                 
            var data = JSON.parse(data.responseText); // here it is giving the error when I checked in the browser console. please help me
             
            for (var i = 0; i < data.length; i++) 
            {
                displayLocation(data[i]);
            }
        });
                
            }
    
            
    function makeRequest(url, callback) {
        var request;
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
            
        } else {
            request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
        }
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
               callback(request);
            }
        }
    
        request.open("GET", url, true);
        request.send();
    }
            
            
    function displayLocation(location) 
    {
    
    
             
        var content =   '<div class="infoWindow"><strong>'  + location.name + '</strong>'
                        + '<br/>'     + location.address
                        + '<br/>'     + location.description + '</div>';
         
        if (parseInt(location.lat) == 0) {
            geocoder.geocode( { 'address': location.address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                     
                    var marker = new google.maps.Marker({
                        map: map, 
                        position: results[0].geometry.location,
                        title: location.name
                    });
                     
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(content);
                        infowindow.open(map,marker);
                    });
                }
            });
        } else {
            var position = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lon));
            var marker = new google.maps.Marker({
                map: map, 
                position: position,
                title: location.name
            });
             
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(content);
                infowindow.open(map,marker);
            });
        }
            }        
        
    
            
        
            </script>
            
        </head>
        
        <body onload="init();">
             
            <h1>Places to check out in Zagreb</h1>
             
            <section id="sidebar">
                <div id="directions_panel"></div>
            </section>
             
            <section id="main">
                <div id="map_canvas" style="width: 70%; height: 500px;"></div>
            </section>
             
        </body>
    </html>
    Please help me out to resolve this issue, I will be grateful to you

    Thanks
    MD.Samiuddin
    Last edited by DaveMaxwell; Feb 19, 2013 at 07:43.

  2. #2
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Is 'locations.php' located in the same folder as this script?

    Try this:
    Code:
    makeRequest('locations.php', function(data){
    
         console.log(data); // check what data contains
    
         var data = JSON.parse(data.responseText); // here it is giving the error when I checked in the browser console. please help me
         for (var i = 0; i < data.length; i++){
                displayLocation(data[i]);
         }
    });
    Check that data is an object that contains responseText.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mickyginger View Post
    Is 'locations.php' located in the same folder as this script?

    Try this:
    Code:
    makeRequest('locations.php', function(data){
    
         console.log(data); // check what data contains
    
         var data = JSON.parse(data.responseText); // here it is giving the error when I checked in the browser console. please help me
         for (var i = 0; i < data.length; i++){
                displayLocation(data[i]);
         }
    });
    Check that data is an object that contains responseText.


    I tried your suggestion and I got this in the response text


    Code:
    XMLHttpRequest {statusText: "OK", status: 200, response: "{"0":"1","id":"1","1":"Archaeological Museum","nam…hs that seem a world away from bustling Zagreb."}", responseType: "", responseXML: null…}
    onabort: null
    onerror: null
    onload: null
    onloadend: null
    onloadstart: null
    onprogress: null
    onreadystatechange: function () {
    readyState: 4
    response: "{"0":"1","id":"1","1":"Archaeological Museum","name":"Archaeological Museum","2":null,"address":null,"3":"45.823704","lat":"45.823704","4":"15.990757","lon":"15.990757","5":"The Archaeological Museum in Zagreb, Croatia is an archaeological museum with over 450,000 varied artifacts and monuments, gathered from various sources.","description":"The Archaeological Museum in Zagreb, Croatia is an archaeological museum with over 450,000 varied artifacts and monuments, gathered from various sources."}{"0":"2","id":"2","1":"Modern Gallery","name":"Modern Gallery","2":"Andrije Hebranga 1, 10000, Zagreb, Croatia","address":"Andrije Hebranga 1, 10000, Zagreb, Croatia","3":"45.809280","lat":"45.809280","4":"15.977570","lon":"15.977570","5":"Modern Gallery is a museum in Zagreb, Croatia that holds the most important and comprehensive collection of paintings, sculptures and drawings by 19th and 20th century Croatian artists.","description":"Modern Gallery is a museum in Zagreb, Croatia that holds the most important and comprehensive collection of paintings, sculptures and drawings by 19th and 20th century Croatian artists."}{"0":"3","id":"3","1":"Technical Museum","name":"Technical Museum","2":"Savska cesta 18, 10000, Zagreb, Croatia","address":"Savska cesta 18, 10000, Zagreb, Croatia","3":"45.803555","lat":"45.803555","4":"15.965023","lon":"15.965023","5":"The museum was founded in 1954 and it maintains the oldest preserved steam engine in the area, dating from the mid-19th century, which is still operational.","description":"The museum was founded in 1954 and it maintains the oldest preserved steam engine in the area, dating from the mid-19th century, which is still operational."}{"0":"4","id":"4","1":"St. Mark's Church","name":"St. Mark's Church","2":"Saint Mark's Square 5, 10000, Zagreb, Croatia","address":"Saint Mark's Square 5, 10000, Zagreb, Croatia","3":"45.816677","lat":"45.816677","4":"15.973806","lon":"15.973806","5":"The Romanesque window found in its south facade is the best evidence that the church must have been built as early as the 13th century as is also the semicircular groundplan of St. Mary's chapel","description":"The Romanesque window found in its south facade is the best evidence that the church must have been built as early as the 13th century as is also the semicircular groundplan of St. Mary's chapel"}{"0":"5","id":"5","1":"Zagreb Cathedral","name":"Zagreb Cathedral","2":"Kaptol 31, 10000, Zagreb, Croatia","address":"Kaptol 31, 10000, Zagreb, Croatia","3":"45.816723","lat":"45.816723","4":"15.978199","lon":"15.978199","5":"Zagreb Cathedral on Kaptol is the most famous building in Zagreb, and the tallest building in Croatia.","description":"Zagreb Cathedral on Kaptol is the most famous building in Zagreb, and the tallest building in Croatia."}{"0":"6","id":"6","1":"The Grounded Sun","name":"The Grounded Sun","2":"Bogovi?eva, 10000, Zagreb, Croatia","address":"Bogovi?eva, 10000, Zagreb, Croatia","3":"45.812180","lat":"45.812180","4":"15.975432","lon":"15.975432","5":"Nine Views is an ambiental installation in Zagreb, Croatia which, together with the sculpture The Grounded Sun, makes up a consistent model of solar system.","description":"Nine Views is an ambiental installation in Zagreb, Croatia which, together with the sculpture The Grounded Sun, makes up a consistent model of solar system."}{"0":"7","id":"7","1":"Croatian National Theatre","name":"Croatian National Theatre","2":"Marshal Tito Square 15, 10000, Zagreb, Croatia","address":"Marshal Tito Square 15, 10000, Zagreb, Croatia","3":"45.810184","lat":"45.810184","4":"15.970123","lon":"15.970123","5":"The Croatian National Theatre in Zagreb is a theatre located in Zagreb, owned and operated by the Ministry of Culture","description":"The Croatian National Theatre in Zagreb is a theatre located in Zagreb, owned and operated by the Ministry of Culture"}{"0":"8","id":"8","1":"Museum of Contemporary Art","name":"Museum of Contemporary Art","2":"Dubrovnik Avenue 17, 10000, Zagreb, Croatia","address":"Dubrovnik Avenue 17, 10000, Zagreb, Croatia","3":"45.777695","lat":"45.777695","4":"15.977888","lon":"15.977888","5":"The museum traces its origins from the City Gallery of Contemporary Art which was established in 1954.","description":"The museum traces its origins from the City Gallery of Contemporary Art which was established in 1954."}{"0":"9","id":"9","1":"Maksimir Park","name":"Maksimir Park","2":"Maksimir Park, 10000, Zagreb, Croatia","address":"Maksimir Park, 10000, Zagreb, Croatia","3":"45.829090","lat":"45.829090","4":"16.019424","lon":"16.019424","5":"Maksimir Park features artificial lakes, an Echo Pavilion and the 19th century Bellevue Pavilion.","description":"Maksimir Park features artificial lakes, an Echo Pavilion and the 19th century Bellevue Pavilion."}{"0":"10","id":"10","1":"Mimara Museum","name":"Mimara Museum","2":"Trg Franklina Delanoa Roosevelta 5, 10000, Zagreb, Croatia","address":"Trg Franklina Delanoa Roosevelta 5, 10000, Zagreb, Croatia","3":"0.000000","lat":"0.000000","4":"0.000000","lon":"0.000000","5":"It is located in a late 19th century neo-Renaissance palace. The holdings comprise 3,750 works of art of various techniques and materials, and different cultures and civilizations.","description":"It is located in a late 19th century neo-Renaissance palace. The holdings comprise 3,750 works of art of various techniques and materials, and different cultures and civilizations."}{"0":"11","id":"11","1":"Strossmayer Gallery of Old Masters","name":"Strossmayer Gallery of Old Masters","2":null,"address":null,"3":"45.823704","lat":"45.823704","4":"15.990757","lon":"15.990757","5":"The Strossmayer Gallery holding includes around 4,000 works, of which some 250 are on display, with the remainder in storage, or on display at other museums or gallery institutions in Croatia.","description":"The Strossmayer Gallery holding includes around 4,000 works, of which some 250 are on display, with the remainder in storage, or on display at other museums or gallery institutions in Croatia."}{"0":"12","id":"12","1":"Vatroslav Lisinski Concert Hall","name":"Vatroslav Lisinski Concert Hall","2":"Stjepan Radi? Square 4, 10000, Zagreb, Croatia","address":"Stjepan Radi? Square 4, 10000, Zagreb, Croatia","3":"0.000000","lat":"0.000000","4":"0.000000","lon":"0.000000","5":"Vatroslav Lisinski Concert Hall is a large concert hall and convention center in Zagreb, Croatia. It is named after Vatroslav Lisinski, a 19th-century Croatian composer.","description":"Vatroslav Lisinski Concert Hall is a large concert hall and convention center in Zagreb, Croatia. It is named after Vatroslav Lisinski, a 19th-century Croatian composer."}{"0":"13","id":"13","1":null,"name":null,"2":"Dverce, 10000, Zagreb, Croatia","address":"Dverce, 10000, Zagreb, Croatia","3":"45.814510","lat":"45.814510","4":"15.973316","lon":"15.973316","5":"The tower, which dates to the 13th century, was built to guard the southern gate of the Gradec town wall.","description":"The tower, which dates to the 13th century, was built to guard the southern gate of the Gradec town wall."}{"0":"14","id":"14","1":"Jarun Lake","name":"Jarun Lake","2":"Jarun Lake, 10000, Zagreb, Croatia","address":"Jarun Lake, 10000, Zagreb, Croatia","3":"45.783333","lat":"45.783333","4":"15.916667","lon":"15.916667","5":"Jarun Lake is a popular getaway for residents any time of year but especially in the summer when the clear waters are ideal for swimming.","description":"Jarun Lake is a popular getaway for residents any time of year but especially in the summer when the clear waters are ideal for swimming."}{"0":"15","id":"15","1":"Botanical Garden","name":"Botanical Garden","2":"Trg Marka Marulica 9a, Zagreb, Croatia (Botanical Gardens)","address":"Trg Marka Marulica 9a, Zagreb, Croatia (Botanical Gardens)","3":"0.000000","lat":"0.000000","4":"0.000000","lon":"0.000000","5":"In addition to 10,000 species of plant, including 1800 tropical flora specimens, the landscaping has created restful corners and paths that seem a world away from bustling Zagreb.","description":"In addition to 10,000 species of plant, including 1800 tropical flora specimens, the landscaping has created restful corners and paths that seem a world away from bustling Zagreb."}"
    responseText: "{"0":"1","id":"1","1":"Archaeological Museum","name":"Archaeological Museum","2":null,"address":null,"3":"45.823704","lat":"45.823704","4":"15.990757","lon":"15.990757","5":"The Archaeological Museum in Zagreb, Croatia is an archaeological museum with over 450,000 varied artifacts and monuments, gathered from various sources.","description":"The Archaeological Museum in Zagreb, Croatia is an archaeological museum with over 450,000 varied artifacts and monuments, gathered from various sources."}{"0":"2","id":"2","1":"Modern Gallery","name":"Modern Gallery","2":"Andrije Hebranga 1, 10000, Zagreb, Croatia","address":"Andrije Hebranga 1, 10000, Zagreb, Croatia","3":"45.809280","lat":"45.809280","4":"15.977570","lon":"15.977570","5":"Modern Gallery is a museum in Zagreb, Croatia that holds the most important and comprehensive collection of paintings, sculptures and drawings by 19th and 20th century Croatian artists.","description":"Modern Gallery is a museum in Zagreb, Croatia that holds the most important and comprehensive collection of paintings, sculptures and drawings by 19th and 20th century Croatian artists."}{"0":"3","id":"3","1":"Technical Museum","name":"Technical Museum","2":"Savska cesta 18, 10000, Zagreb, Croatia","address":"Savska cesta 18, 10000, Zagreb, Croatia","3":"45.803555","lat":"45.803555","4":"15.965023","lon":"15.965023","5":"The museum was founded in 1954 and it maintains the oldest preserved steam engine in the area, dating from the mid-19th century, which is still operational.","description":"The museum was founded in 1954 and it maintains the oldest preserved steam engine in the area, dating from the mid-19th century, which is still operational."}{"0":"4","id":"4","1":"St. Mark's Church","name":"St. Mark's Church","2":"Saint Mark's Square 5, 10000, Zagreb, Croatia","address":"Saint Mark's Square 5, 10000, Zagreb, Croatia","3":"45.816677","lat":"45.816677","4":"15.973806","lon":"15.973806","5":"The Romanesque window found in its south facade is the best evidence that the church must have been built as early as the 13th century as is also the semicircular groundplan of St. Mary's chapel","description":"The Romanesque window found in its south facade is the best evidence that the church must have been built as early as the 13th century as is also the semicircular groundplan of St. Mary's chapel"}{"0":"5","id":"5","1":"Zagreb Cathedral","name":"Zagreb Cathedral","2":"Kaptol 31, 10000, Zagreb, Croatia","address":"Kaptol 31, 10000, Zagreb, Croatia","3":"45.816723","lat":"45.816723","4":"15.978199","lon":"15.978199","5":"Zagreb Cathedral on Kaptol is the most famous building in Zagreb, and the tallest building in Croatia.","description":"Zagreb Cathedral on Kaptol is the most famous building in Zagreb, and the tallest building in Croatia."}{"0":"6","id":"6","1":"The Grounded Sun","name":"The Grounded Sun","2":"Bogovi?eva, 10000, Zagreb, Croatia","address":"Bogovi?eva, 10000, Zagreb, Croatia","3":"45.812180","lat":"45.812180","4":"15.975432","lon":"15.975432","5":"Nine Views is an ambiental installation in Zagreb, Croatia which, together with the sculpture The Grounded Sun, makes up a consistent model of solar system.","description":"Nine Views is an ambiental installation in Zagreb, Croatia which, together with the sculpture The Grounded Sun, makes up a consistent model of solar system."}{"0":"7","id":"7","1":"Croatian National Theatre","name":"Croatian National Theatre","2":"Marshal Tito Square 15, 10000, Zagreb, Croatia","address":"Marshal Tito Square 15, 10000, Zagreb, Croatia","3":"45.810184","lat":"45.810184","4":"15.970123","lon":"15.970123","5":"The Croatian National Theatre in Zagreb is a theatre located in Zagreb, owned and operated by the Ministry of Culture","description":"The Croatian National Theatre in Zagreb is a theatre located in Zagreb, owned and operated by the Ministry of Culture"}{"0":"8","id":"8","1":"Museum of Contemporary Art","name":"Museum of Contemporary Art","2":"Dubrovnik Avenue 17, 10000, Zagreb, Croatia","address":"Dubrovnik Avenue 17, 10000, Zagreb, Croatia","3":"45.777695","lat":"45.777695","4":"15.977888","lon":"15.977888","5":"The museum traces its origins from the City Gallery of Contemporary Art which was established in 1954.","description":"The museum traces its origins from the City Gallery of Contemporary Art which was established in 1954."}{"0":"9","id":"9","1":"Maksimir Park","name":"Maksimir Park","2":"Maksimir Park, 10000, Zagreb, Croatia","address":"Maksimir Park, 10000, Zagreb, Croatia","3":"45.829090","lat":"45.829090","4":"16.019424","lon":"16.019424","5":"Maksimir Park features artificial lakes, an Echo Pavilion and the 19th century Bellevue Pavilion.","description":"Maksimir Park features artificial lakes, an Echo Pavilion and the 19th century Bellevue Pavilion."}{"0":"10","id":"10","1":"Mimara Museum","name":"Mimara Museum","2":"Trg Franklina Delanoa Roosevelta 5, 10000, Zagreb, Croatia","address":"Trg Franklina Delanoa Roosevelta 5, 10000, Zagreb, Croatia","3":"0.000000","lat":"0.000000","4":"0.000000","lon":"0.000000","5":"It is located in a late 19th century neo-Renaissance palace. The holdings comprise 3,750 works of art of various techniques and materials, and different cultures and civilizations.","description":"It is located in a late 19th century neo-Renaissance palace. The holdings comprise 3,750 works of art of various techniques and materials, and different cultures and civilizations."}{"0":"11","id":"11","1":"Strossmayer Gallery of Old Masters","name":"Strossmayer Gallery of Old Masters","2":null,"address":null,"3":"45.823704","lat":"45.823704","4":"15.990757","lon":"15.990757","5":"The Strossmayer Gallery holding includes around 4,000 works, of which some 250 are on display, with the remainder in storage, or on display at other museums or gallery institutions in Croatia.","description":"The Strossmayer Gallery holding includes around 4,000 works, of which some 250 are on display, with the remainder in storage, or on display at other museums or gallery institutions in Croatia."}{"0":"12","id":"12","1":"Vatroslav Lisinski Concert Hall","name":"Vatroslav Lisinski Concert Hall","2":"Stjepan Radi? Square 4, 10000, Zagreb, Croatia","address":"Stjepan Radi? Square 4, 10000, Zagreb, Croatia","3":"0.000000","lat":"0.000000","4":"0.000000","lon":"0.000000","5":"Vatroslav Lisinski Concert Hall is a large concert hall and convention center in Zagreb, Croatia. It is named after Vatroslav Lisinski, a 19th-century Croatian composer.","description":"Vatroslav Lisinski Concert Hall is a large concert hall and convention center in Zagreb, Croatia. It is named after Vatroslav Lisinski, a 19th-century Croatian composer."}{"0":"13","id":"13","1":null,"name":null,"2":"Dverce, 10000, Zagreb, Croatia","address":"Dverce, 10000, Zagreb, Croatia","3":"45.814510","lat":"45.814510","4":"15.973316","lon":"15.973316","5":"The tower, which dates to the 13th century, was built to guard the southern gate of the Gradec town wall.","description":"The tower, which dates to the 13th century, was built to guard the southern gate of the Gradec town wall."}{"0":"14","id":"14","1":"Jarun Lake","name":"Jarun Lake","2":"Jarun Lake, 10000, Zagreb, Croatia","address":"Jarun Lake, 10000, Zagreb, Croatia","3":"45.783333","lat":"45.783333","4":"15.916667","lon":"15.916667","5":"Jarun Lake is a popular getaway for residents any time of year but especially in the summer when the clear waters are ideal for swimming.","description":"Jarun Lake is a popular getaway for residents any time of year but especially in the summer when the clear waters are ideal for swimming."}{"0":"15","id":"15","1":"Botanical Garden","name":"Botanical Garden","2":"Trg Marka Marulica 9a, Zagreb, Croatia (Botanical Gardens)","address":"Trg Marka Marulica 9a, Zagreb, Croatia (Botanical Gardens)","3":"0.000000","lat":"0.000000","4":"0.000000","lon":"0.000000","5":"In addition to 10,000 species of plant, including 1800 tropical flora specimens, the landscaping has created restful corners and paths that seem a world away from bustling Zagreb.","description":"In addition to 10,000 species of plant, including 1800 tropical flora specimens, the landscaping has created restful corners and paths that seem a world away from bustling Zagreb."}"
    responseType: ""
    responseXML: null
    status: 200
    statusText: "OK"
    upload: XMLHttpRequestUpload
    withCredentials: false
    __proto__: XMLHttpRequest
    
    Uncaught SyntaxError: Unexpected token {
    please help me out to resolve the issue
    Last edited by SpacePhoenix; Feb 20, 2013 at 01:39.

  4. #4
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,078
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    When you use code in your posts, please use the appropriate code tags.
    You can do this by clicking "Go Advanced" in the editor, highlighting the code you want to put in tags and then selecting the correct syntax from the 'syntax' dropdown.
    If you look on the right hand side of the second row of the advanced view, you have three shortcuts for 'code' 'html' and 'php' respectively.

    Regarding your issue, this is your problem:

    Code:
    Uncaught SyntaxError: Unexpected token {
    You either have a syntax error in your JavaScript, or your PHP script is returning malformed JSON.

    Can you paste the function you are using in the PHP script to serve up the JSON?

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Pullo

    Thanks for your reply, This is the code in locations.php

    Code:
    <?php
    $con = mysql_connect("localhost","test","test");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("maptest", $con);
    
    $result = mysql_query("SELECT * FROM locations");
    
    while($row = mysql_fetch_array($result))
      {
     echo json_encode( $row );
      }
    
    mysql_close($con);
    ?>

    please help me to resolve the issue

  6. #6
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I think the error Uncaught SyntaxError: Unexpected token { is probably coming from javascript... Does the console give you this error? If so is there a line number?

    I've checked the code you posted and I can see any errors...

    Do you have this online somewhere?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •