错误200 - SyntaxError:JSON.parse:意外字符

时间:2014-03-13 01:52:37

标签: php mysql json

我正在尝试使用ajax和jquery从mysql数据库填充下拉列表。数据应该是json格式。但我不断收到语法错误:JSON.parse:意外的字符。请指教。谢谢你的帮助。

这是我在Dreamweaver中的代码

 $(document).ready(function(){

$.getJSON("http://localhost:8000/states.php", function(data) {

    $.each(jsondata, function(index, item) {

        $('<option></option>').val(item.state_code).html(item.state).appendTo("select#personalstate");
        }); 
    }); 
});

数据库代码:

database.php中

<?php
$dsn = 'mysql:host=localhost;dbname=elihu';
$username = 'admin';
$password = 'password';
$options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
try {
    $db = new PDO($dsn, $username, $password, $options);
} catch (PDOException $e) {
    $error_message = $e->getMessage();
    include ('index.php');
    exit();
}
?>

json php文件

<?php

require('database.php');

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

    try {
        $statement = $db->prepare($query);
        $statement->execute();
        $states = $statement->fetchAll(PDO::FETCH_ASSOC);
        $statement->closeCursor();   
        echo json_encode($states);

    } catch (PDOException $e) {
        $error_message = $e->getMessage();
        include ('index.php');
        exit();
    }
}
?>

1 个答案:

答案 0 :(得分:0)

$.getJSON("Model/php/states.php", function(data) {
    // No need to parse the json data, it's already been parsed by getJSON
    // jsondata = $.parseJSON(data);