[問題] 從 jsp 傳 json 到 javascript

看板Ajax作者 (風)時間11年前 (2012/10/01 22:28), 編輯推噓1(109)
留言10則, 3人參與, 最新討論串1/1
已經照T大改了 可是還是不能@@ 已經用 chrome 的 debugger 去看 顯示以下錯誤訊息 http://gyazo.com/f979d13ad312072e32b1580a967fc343 我的 javascript function handler() { if(AJAX.readyState == 4 && AJAX.status == 200) { var json = eval('(' + AJAX.responseText +')'); alert( json.name[0] + '<BR>' + json.name[1] ); }else if (AJAX.readyState == 4 && AJAX.status != 200) { alert('Something went wrong...'); } } jsp <%@page contentType="text/html; charset=UTF-8"%> <%@page import="org.json.simple.JSONObject"%> <% String []apple = new String[3] ; for ( int i = 0 ; i < 3 ; i++ ) apple[i] = "aa" ; JSONObject obj = new JSONObject(); obj.put( "name", apple ); out.print( obj ); out.flush(); %> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.204.244.108

10/01 22:59, , 1F
丟 array 回來呀 :)
10/01 22:59, 1F

10/02 13:02, , 2F
我有試著把 json 改用 array 來宣告, 可是還是不能
10/02 13:02, 2F

10/02 15:34, , 3F
把你試的結果貼出來啊 XD
10/02 15:34, 3F

10/02 15:35, , 4F
不能有很多種,有時候是邏輯錯誤,有時候是呼叫錯誤。
10/02 15:35, 4F

10/02 15:35, , 5F
這個問題目前最接近的答案是 array 或 list 沒錯。
10/02 15:35, 5F
※ 編輯: wind75609592 來自: 123.204.244.108 (10/02 17:52)

10/02 21:09, , 6F
你要改得是 jsp 的JSONObject ,你去改client 幹麼 XDD
10/02 21:09, 6F

10/02 21:10, , 7F
你 put("name", namelist) 就能從
10/02 21:10, 7F

10/02 21:10, , 8F
json.name[0] / json.name[1] 拿回來啦
10/02 21:10, 8F
※ 編輯: wind75609592 來自: 123.204.244.108 (10/02 22:19) ※ 編輯: wind75609592 來自: 123.204.244.108 (10/02 22:39)

10/02 23:18, , 9F
利用console.log(AJAX.responseText)把JSP傳過來的json
10/02 23:18, 9F

10/02 23:18, , 10F
印出來看看
10/02 23:18, 10F
文章代碼(AID): #1GQQYT5q (Ajax)