ajax 및 jquery를 사용하여 mysql 데이터베이스의 드롭 다운 목록을 채우려고합니다. 데이터는 json 형식이어야합니다. 하지만 구문 오류가 계속 : JSON.parse : 예기치 않은 문자. 제발 조언. 도와 줘서 고마워.오류 200 - SyntaxError : JSON.parse : 예기치 않은 문자
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();
}
}
?>
0 :
이것은
$(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");
});
});
});
데이터베이스 코드 Dreamweaver에서 내 코드입니다
응답에서 원시 JSON을 표시 할 수 있습니까? – Fenton
사이드 노드 : 입력 매개 변수없이 간단한 쿼리를'prepare()/execute() 할 필요는 없습니다. '$ result = $ db-> query ('SELECT * FROM states')를 호출 할 수 있습니다. SteveFenton의 의견을 돕기 위해 Chrome에서 개발자 콘솔을 열고 (네트워크) '경고'(데이터) 또는 '네트워크'탭을보고 응답을 확인할 수 있습니다. $ states = $ result-fetchAll(); ' –
서버가있었습니다. – teynon