【发布时间】:2013-08-29 15:19:45
【问题描述】:
更新:现在我可以看到数据已加载(从 Chrome 上的 F12 > 网络)。但是跨度不会使用从 JSON 加载的数据进行更新:(
我有错误“Uncaught SyntaxError: Unexpected token”,但经过数小时的搜索和测试我不知道为什么。
index.html 头:
<script type="text/javascript">
var displayResult = function(response){
$("#fruit_name").append(response.fruit_name);
$("#fruit_color").append(response.fruit_details.Color);
$("#fruit_taste").append(response.fruit_details.Taste);
}
var response = $.ajax({
type: "GET",
dataType:"json",
url: "https://www.domain.tld/api/?core=fruits&function=getFruits",
data: "",
success: displayResult
});
</script>
index.html 正文:
<p>Fruit name : <span id="fruit_name"></span></p>
<p>Fruit color : <span id="fruit_color"></span></p>
<p>Fruit taste : <span id="fruit_taste"></span></p>
api 文件(PHP):
$array = array(
"fruit_name" => "Tomato",
"fruit_details" => array(
"Color" => "red",
"Taste" => "acid"
)
);
echo json_encode($array,JSON_UNESCAPED_UNICODE);
api 原始返回:
{"fruit_name":"Tomato","fruit_details":{"Color":"red","Taste":"acid"}}
有人可以帮我吗?
内容 JSON 作为 Content-Type: application/json 发送
谢谢。
【问题讨论】:
-
第 4 行和第 5 行缺少分号
-
复制/粘贴时出错,已更新;)
-
看起来不错...这是浏览器控制台指出的那一行
-
你有错误发生位置的行号吗?
-
出现错误的时间和地点
标签: php javascript jquery json