作者Deicide (Deicide)
看板Ajax
标题[问题] json回传undefined
时间Sat Jun 27 15:56:25 2015
不好意思
想请问我使用ajax至php 想做json回传
用json回传後 出来都会是undefind
$("
#select_submit").on('click', function () {
$.ajax({
url:"add.php",
data: $('
#select_from').serialize() ,
type: "POST",
datatype: "json",
success: function(json){
$("#test").html(json);
$("#test2").html(json.total);
alert(json.total);
alert("成功新增");
},
error:function(xhr, ajaxOptions, thrownError){
alert("错误");
}
});
});
add.php
$data = array(
"total" => "321",
"test" => "123"
);
echo json_encode($data);
然後这样 会成功出现的只有 $("#test").html(json); 这一行
出现像这样的JSON格式{"total":"$321","test":"123"}
可是以下的 json.total 这类有指项的 不管怎样都会undefind
也有参考版上前几篇的JSON回传 也没有个所以然.. 想请问我是哪边出了差错?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 49.158.54.142
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1435391788.A.0EB.html
1F:推 eight0: json 大概还是字串 没被转成物件 06/27 16:17
2F:→ itisjoe: add.php 加一个 json type 的 header 试试看 06/27 16:53
3F:→ Deicide: 加了header就好了! 太神了 感谢你们!! 06/27 17:14
4F:→ MangoTW: 加Header或手动decode都可 06/30 01:16