作者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://www.ptt.cc/bbs/Ajax/M.1435391788.A.0EB.html
推 eight0: json 大概還是字串 沒被轉成物件 06/27 16:17
→ itisjoe: add.php 加一個 json type 的 header 試試看 06/27 16:53
→ Deicide: 加了header就好了! 太神了 感謝你們!! 06/27 17:14
→ MangoTW: 加Header或手動decode都可 06/30 01:16