作者tony5566 (5245566)
看板Web_Design
標題[問題] Javascript json to array 轉換
時間Thu Jan 29 21:38:43 2015
var test;
$.post("query.php",{id:id})
.done(function(data){
test = $.parseJSON(data);});
console.log(test);
data是有撈出資料的
但是test一直顯示是undefined
這個問題是什麼呢!?
data內容:
[{"0":"120.95","rest_lon":"120.95","1":"24.8","rest_lat":"24.8",
"2":"\u65b0\u7af9\u5e02\u5927\u5b78\u8def1001\u865f",
"rest_address":"\u65b0\u7af9\u5e02\u5927\u5b78\u8def1001\u865f",
"3":"03-3369880","rest_tel":"03-3369880"},
{"0":"120.96","rest_lon":"120.96","1":"24.81","rest_lat":"24.81",
"2":"\u65b0\u7af9\u7e23\u5927\u5b78\u8def1\u865f",
"rest_address":"\u65b0\u7af9\u7e23\u5927\u5b78\u8def1\u865f",
"3":"03-2571864","rest_tel":"03-2571864"}]
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.134.94
※ 文章網址: https://www.ptt.cc/bbs/Web_Design/M.1422538725.A.37D.html
→ osk2: 把data的內容一起po出來吧 01/29 21:58
推 tw0517tw: 這個應該不用parse可以直接拿來用吧 01/29 22:08
→ tw0517tw: 至少我直接貼到console結果是object不是string 01/29 22:09
→ osk2: 看起來沒錯耶,應該直接把data拿去用不用再parseJSON了吧 01/29 22:11
var test;
$.post("query.php",{id: id})
.done(function(data){
test = data;});
alert(test);
還是一樣test是undefined
主要就是想要把撈出來的資料可以到括號外面使用
※ 編輯: tony5566 (140.113.134.94), 01/29/2015 22:27:34
推 tw0517tw: jQuery.extend()? 01/29 22:33
推 qazwsx9006: 小的淺見...妳在alert(test)的時候,post還沒把data抓 01/29 23:15
→ qazwsx9006: 回來。 非同步概念? 有錯煩請糾正.... 01/29 23:16
推 tw0517tw: 我猜樓上是對的XD 01/29 23:36
→ tony5566: 恩恩 對XD 感謝提醒 01/30 00:07