作者gaekeamql (芋頭)
看板Ajax
標題Re: [問題] 如何使用 jQuery.parseJSON
時間Fri Dec 24 23:17:59 2010
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
1F:→ TonyQ:jsonp 必須要 server 有實做 jsonp 的規格才能用,不是任意 12/24 23:18
2F:→ TonyQ:json 都能用的。 12/24 23:19
3F:→ TonyQ:server 必須有能力根據 callback value 來寫出 var xx={} 12/24 23:19
4F:→ TonyQ:其實 JSONP 是個很 tricky 的東西 有機會應該來討論一下XD 12/24 23:20
5F:→ TonyQ:啊啊 我漏看了你最後兩行 :P 12/24 23:21
6F:→ gaekeamql:哈哈!藏在太後面了..就只要用GET 包住JSON 就可以了 12/24 23:23
7F:推 othree:不過濾一下 $_GET ,蠻危險XD 12/25 00:07