您对使用JSON相当新,并且一直在尝试使用JSON将数组从PHP传递到Javascript。我一直在阅读它,但是在代码的javascripts端的eval函数上有错误。如果有人请请帮助我真的很感谢提前感谢!
PHP(lang.en.php)
<?php
$lang['TITLE'] = 'My Title';
$lang['MESSAGE'] = 'My Message';
?>
HTML / Javascript(index.php)
<?php
include "lang/lang.en.php";
?>
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>My Page</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script language='JavaScript' type='text/javascript'>
$(function(){
//Get the JSON string and pass it to the cb function
$.get("index.php", function(data){cb(data)});
})
function cb(data){
//converts the JSON string to a JavaScript object
eval("lang="+data); //This is where the error occurs
lang = dataToObj;
alert(lang.TITLE);
}
</script>
</head>
<body>
<? json_encode($lang); ?>
</body>
</html>