Re: [問題] 如何使用 jQuery.parseJSON

看板Ajax作者 (芋頭)時間14年前 (2010/12/24 23:18), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串2/3 (看更多)
JSON 不能跨SERVER 只能用JSONP $.getJSON("http://xxxxx/index.php?jsoncallback=?", function(data){ }); ※ 引述《tabear (胖熊)》之銘言: : http://api.jquery.com/jQuery.parseJSON/ : 我根據這個網站教學 但是網頁回傳找不到 jQuery.parseJSON 這個function : 我是從 A.php送到-->B.php再傳回-->A.php : 我有看回傳值,回傳的很正確 但是無法解析json 請問這如何解決?? : A.php : <script type="text/javascript" src="jquery.js"></script> : <script type='text/javascript'> : abc(); : function abc(){ : createRequest(); : var url='B.php'; : post_str='aaa=1'; : request.onreadystatechange=abc_res; : request.open('POST',url,true); : request.setRequestHeader("Content-Type","application/x-www : -form-urlencoded;charset=UTF-8"); : request.send(post_str); : } : function abc_res(){ : if(request.readyState==4){ : if(request.status==200){ : 這行出錯==> var orgData=jQuery.parseJSON(request.responseText); : .... : } : } : } : </script> : B.php 程式如下 : if(isset($_POST['aaa'])){ : $query="SELECT * FROM a_table"; : $res=$db->query($query); : $his_data=array(); : while($data=$res->fetch()){ : array_push($his_data,$data); : } : echo json_encode($his_data); : } json_en_c = json_encode($his_data); jsonp 過來需要用 echo $_GET['jsoncallback']."(".json_en_c.")"; -- █   █         █ http://www.u-tao.tw █   █ ███  ▄▄█▄ ▄ ▄ █   █ █ █ ▉ █ ▉ █   █ █ █ ▉ █ ▉ ◥▇▇▇◤ █ ◢ █ ▋▊ █ ▉ ██ ▊█ ◥ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.105.192.152

12/24 23:18, , 1F
jsonp 必須要 server 有實做 jsonp 的規格才能用,不是任意
12/24 23:18, 1F

12/24 23:19, , 2F
json 都能用的。
12/24 23:19, 2F

12/24 23:19, , 3F
server 必須有能力根據 callback value 來寫出 var xx={}
12/24 23:19, 3F

12/24 23:20, , 4F
其實 JSONP 是個很 tricky 的東西 有機會應該來討論一下XD
12/24 23:20, 4F

12/24 23:21, , 5F
啊啊 我漏看了你最後兩行 :P
12/24 23:21, 5F

12/24 23:23, , 6F
哈哈!藏在太後面了..就只要用GET 包住JSON 就可以了
12/24 23:23, 6F

12/25 00:07, , 7F
不過濾一下 $_GET ,蠻危險XD
12/25 00:07, 7F
文章代碼(AID): #1D5Bchu8 (Ajax)
討論串 (同標題文章)
文章代碼(AID): #1D5Bchu8 (Ajax)