내 웹 사이트에 json을 삽입하는 데 문제가 있습니다.Javascript/jQuery로 JSON 구문 분석
입니다 HTML과 자바 스크립트 : 콘솔에서
<!doctype html>
<html lang="us">
<head>
<meta charset="utf-8">
<title>jQuery UI Example Page</title>
<link href="css/custom-theme/jquery-ui-1.10.0.custom.css" rel="stylesheet">
<script src="js/jquery-1.9.0.js"></script>
<script src="js/jquery-ui-1.10.0.custom.js"></script>
<script>
function isJSON(str) {
try {
JSON.parse(str)
} catch (e) {
return false
}
return true
}
jQuery(function(){
<?
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
?>
data = <?= json_encode($arr) ?>
if (isJSON(data)) {
json = jQuery.parseJSON(data)
console.log(json)
} else {
console.log("Not a json.")
console.log(data)
console.log(jQuery.parseJSON(data))
}
})
</script>
<body>
</body>
</html>
이 출력 다음
Not a json. filename.php:29
Object {a: 1, b: 2, c: 3, d: 4, e: 5} filename.php:30
Uncaught SyntaxError: Unexpected token o
긴 하루를 이미 왔으며 대부분의 아마 (희망) 내가 뭔가를 분명 누락. 나는 생성 된 json의 유효성을 확인하려고 시도했다 : http://jsonlint.com/ 그리고 그들의 사이트에 따르면 그것은 유효했다.
미리 감사드립니다.
피 볼 수 있습니다. – BenM
@ BenM 왜? - 나는 그 (것)들에 결코 문제가 없었다. – Krabats
@krabats : 모든 서버에서 기본적으로 사용하도록 설정되어 있지 않습니다. – cdmckay