dongpian6319 2015-04-27 17:07
浏览 158
已采纳

php / ajax JSON响应一旦解析就返回[object,Object]

When I try to parse a response from an AJAX request as JSON, I get [object, Object] values instead of the actual ones returned before parsing the response. What could I have done wrong?

I have one script named "apply.php" which has the a short application and an AJAX request called when the user selects a town. There is another script named "suburb.php" which retrieves stored suburbs in the database that are under the selected town. The second script is called when the users selects/changes a town.

In the "apply.php" script I have a JavaScript alert that display the response, which I then parses as JSON. After passing the response, [object, Object] is returned instead of the actual values, thus disabling me from reading the JSON key values.

Using the values without parsing them does not help either. I tried checking for question that relate to my problem without any luck.

Your assistance will be greatly appreciated.

apply.php

<?php
$dbcon = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Accomodation Application</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<style>
form div {
    margin-bottom: 20px;
}
</style>
<body>
<div>
    <div>
    <form>
        <p><a href="http://localhost/testwork/">Refresh</a></p>
        <div><label for="applicant-name">First Name</label><br /><input type="text" name="name" id="applicant-name"></div>
        <div><label for="applicant-surname">Surname</label><br /><input type="text" name="surname" id="applicant-surname"></div>
        <div><label for="applicant-identity">Smart ID Number</label><br /><input type="text" name="surname" id="applicant-identity"></div>
        <div id="town">
            <label for="applicant-town">Town</label><br />
            <select name="town" id="applicant-town"><option value="0">-- Select Town --</option>
            <?php
            $towns = mysqli_query($dbcon, "SELECT town_id, town_name FROM towns");
            while($town = mysqli_fetch_array($towns)){
                $town_id = $town['town_id'];
                $town_name = $town['town_name'];
                echo("<option value=\"$town_id\">$town_name</option>");
            }?>
            </select>
        </div>
        <div id="suburb">
            <label for="applicant-suburb">Suburb</label><br />
            <select name="suburb" id="applicant-suburb">
                <option value="0">-- Select Suburb --</option>
            </select>
        </div>
        <button type="submit">Submit</button>
    </form>
    </div>
</div>
<script>
$(document).ready(function(){
    $('#applicant-town').change(function(){
        var suburb_town = $(this).val();
        $.ajax({
            type: "GET",
            url: "suburbs.php",
            data: {town: suburb_town}
        })
        .done(function(data){
            alert(data);
            var burbs = JSON.parse(data);
            alert(burbs); // e.g. [{"1":"Halfway House"},{"2":"Noordwyk"},{"3":"Vorna Valley"}]
            $(burbs).each(burbs, function(key, value){
                $('#applicant-suburb').append('<option value="'+key+'">'+value+'</option>');
            });
        })
        .fail(function(){
            alert('There is an error somewhere.');
        });
    });
});
</script>
</body>
</html>

suburbs.php

<?php
$dbcon = new mysqli (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(isset($_GET['town'])){
    $town_suburbs = array();
    $town_id = htmlspecialchars($_GET['town']);
    $suburbs = mysqli_query($dbcon, "SELECT suburb_id, suburb_name FROM suburbs WHERE suburb_town = $town_id");
    while($suburb = mysqli_fetch_array($suburbs)){
        $suburb_id = $suburb['suburb_id'];
        $suburb_name = $suburb['suburb_name'];
        $town_suburbs[] = array($suburb_id => $suburb_name);
    }
    echo json_encode($town_suburbs);
}
?>
  • 写回答

6条回答 默认 最新

  • dsjws44266 2015-04-27 17:36
    关注

    So first off from the comments you can see what the issue is. However, you may want to use your "id" for something so with that in mind you could do the following.

    $town_suburbs[] = array($suburb_id => $suburb_name);
    

    You could change that to

    array_push($town_suburbs, array("id" => $suburb_id, "name" => $suburb_name));
    

    Then on your javascript side...

    $(burbs).each(burbs, function(key, value){
        $('#applicant-suburb').append('<option value="'+key+'">'+value+'</option>');
    });
    

    That could change to:

    $(burbs).each(function(){
        $('#applicant-suburb').append('<option value="'+ this.id +'">'+ this.name +'</option>');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?