Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

SOLUTION: After a teamviewer session with @skobaljic he figured out that I was not actually opening the html in localhost but using the file system (as in file://...). I apologize for wasting everyone's time like that.

I am trying to send some php arrays through Ajax and print them out and despite getting a 200 OK response, there is no actual effect in the received html. The status text says "parsererror".

The files are the following:

<!DOCTYPE html>
<html>
<head>
<title>Page Title Woo!</title>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'></script>
</head>
<body>

<h1>Heading</h1>
<p>Paragraph.</p>


        <ul></ul>

        <script type='text/javascript'>
        $(document).ready(function(){

            $.getJSON('DbGetter.php', function(data) {


                    console.log(data);
                    $.each(data, function(key, array) {
                            $('ul').append('<li id="' + key + '">' 
                            + array.longitude + ' ' 
                            + array.latitude + '</li>');
                    });
            })
            .fail(function(error) {
                    console.log(error);
            });

        });
        </script>
</body>
</html> 

and the php:

<?php
$servername = "localhost";
$username = "testuser";
$password = "password";
$dbname = "Locations";

header('Content-Type: application/json');

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM places";
$result = $conn->query($sql);

if ($result->num_rows > 0) {

    //declare associative array
    $array = array();
    $num = 0;

    // output data of each row
    while($row = $result->fetch_assoc()) {

        //store them in an array
        $place = array(
        'id' => $row["id"], 
        'latitude'=> $row["latitude"] , 
        'longitude'=> $row["longitude"], 
        'place_name' => $row["place_name"],
        'country_code'=> $row["country_code"], 
        'postal_code'=> $row["postal_code"]);


        /*
        echo "Coordinates: " . $row["latitude"]. " " . $row["longitude"]. " - Name: " . $row["place_name"]. "   " .  "<br>";
        */

        //building the second associative array
        $array[$num] = $place;
        $num += 1;

    }

        echo json_encode($array);
} else {
    echo json_encode("0 results");
}
$conn->close();
?> 

I've tried looking at the value through firebug, but either I'm blind or it's just not stored anywhere in the DOM. I'm pretty new to web application in general so I don't know how to go about debugging it.

Running the php by itself I get: [{"id":"1","latitude":"57.0502","longitude":"9.9173","place_name":"1000fryd","c??ountry_code":"DK","postal_code":"9000"},{"id":"2","latitude":"58.0502","longitude??":"10.9173","place_name":"same_place","country_code":"DK","postal_code":"9000"}] Which are the 2 rows I expect.

There is also no XHR request marked by Firebug.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
188 views
Welcome To Ask or Share your Answers For Others

1 Answer

You're not encoding valid JSON here.. This will give you a "parseerror" if you try using JQuery's $getJSON with it. Notice the JQuery Ajax Source from lines 275-281.

echo json_encode("0 results");

You could probably change it to something like:

echo json_encode(["0 results"]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...