作者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