【发布时间】:2017-09-16 23:21:11
【问题描述】:
index.php
<button class="button">color</button>
<div class="blue"></div>
<div class="red"></div>
script.js
$(".button").click(function(){
$.ajax({
url: "update.php",
type: "POST",
success: function (data) {
$(".blue").html(data.blue);
$(".red").html(data.red);
alert("success");
}
})
});
更新.php
$array['blue'] = "blue content";
$array['red'] = "red content";
header('Content-type: application/json');
echo json_encode($array);
我想在 localhost 上使用 json。但它不起作用。 我正在使用 MAMP。
【问题讨论】:
-
@Jarla 遇到什么错误?
-
你不需要使用 header('Content-type: application/json');在 update.php 和 ajax 中,你需要在使用数据值之前使用 JSON.parse(data)
-
您遇到了哪些错误/异常?
-
@vSugumar 您确实需要 application/json 内容类型,因为该应用程序提供 json。删除它没有意义
-
我认为它被否决了,因为问题不包含错误;)。没有明确的例外,很难提供帮助。
标签: php json ajax localhost mamp