简体   繁体   中英

error 200 - SyntaxError: JSON.parse: unexpected character

I am trying to fill a dropdown list from a mysql database by using ajax and jquery. The data is should be in json format. But I keep getting syntax error: JSON.parse: unexpected character. Please advise. Thank you for your help.

This is my code in 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 code:

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 file

<?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();
    }
}
?>
$.getJSON("Model/php/states.php", function(data) {
    // No need to parse the json data, it's already been parsed by getJSON
    // jsondata = $.parseJSON(data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM