JSON.parse意外的数据结束错误

时间:2014-03-21 00:48:11

标签: javascript php jquery mysql json

我正在尝试使用php和jquery ajax从mysql数据库加载数据来填充下拉列表。但我一直在firefox和chrome中遇到错误。 firefox中的错误是JSON.parse意外的数据结束和chrome,语法错误意外结束输入。我检查了chrome中的网络选项卡,但没有响应数据。请指教。谢谢。

elihu.js

$(document).ready(function(){   
    $.ajax({            
        type: "POST",
        contentType:"application/json",         
        dataType:"json",                        
        url:"http://localhost/josiejay/states.php",
        success: function(data) { 
            $("#personalstate").append("<option value='" + data.state_code + "'>" + data.state + "</option>");
        },
        error: function(xhr, status, error) {
            alert("Error: " + xhr.status + " - " + error);
        }
    }); 
}); 

states.php

<?php


require('database.php');

function getStates() {
    global $db;
    $query = 'SELECT * FROM states';

    try {
        $statement = $db->prepare($query);
        $statement->execute();
        $states = $statement->fetchall();
        $statement->closeCursor(); 

        $json_data = json_encode($states); 
        echo $json_data;
    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        exit();
    }
}
?>

0 个答案:

没有答案