json

来源:互联网 发布:淘宝如何做详情页 编辑:程序博客网 时间:2024/06/06 05:54

一.apache配置多域名

在apache的conf目录下找到httpd.conf,然后在该文件最后增加如下内容:

# 声明使用虚拟主机过滤规则

NameVirtualHost*:80

#虚拟主机localhost

<VirtualHost*:80>

ServerName  localhost

DocumentRoot"D:\xampp\htdocs\www"

</VirtualHost>

#虚拟主机test.mw.com

<VirtualHost*:80>

ServerName  test.mw.com

DocumentRoot"D:\xampp\htdocs\test.mw.com"

</VirtualHost>

其中第一个是保证原有的localhost还可以继续工作,第二个为新加域名test.mw.com。

然后再修改host文件(在C:\Windows\System32\drivers\etc\)

增加:

#  test.mw.com

127.0.0.1test.mw.com

注意修改httpd.conf后要重启下apache服务。

将《JSON进阶第二篇 AJAX方式传递JSON数据》文中的json2.php拷贝到D:\xampp\htdocs\test.mw.com目录中再在浏览器中输入网址http://test.mw.com/json2.php

运行结果如下:

在浏览器中能看到如上所示的JSON字符串说明已经成功为apache增加了新的域名,接下来就来体验下JSON的跨域名问题。

将json2.html中“$.post("json2.php", {}, function(data){”的“json2.php”改成“http://test.mw.com/json2.php”表示我们要引用test.mw.com域名下的json2.php文件。然后在json2.html中点击按钮,可以发现没任何效果。

用Firefox的Firebug查看点击按钮后AJAX响应的详细过程,可以看到AJAX请求信息已经发送到服务器上了(就是那个127.0.0.1:80),但因为安全性问题,服务器返回的数据被屏蔽了——这就是大名鼎鼎的跨域问题

二.JSONP——解决JSON跨域问题

使用JSONP(JSON with Padding)就可以解决JSON的跨域问题,JSONP的原理可以访问http://zh.wikipedia.org/zh-cn/JSONP简单的说就是利用了<script>标签拥有的开放策略(相对于同域策略)。下面介绍如何使用JSONP。

JSONP的使用分为二步:

第一将html文件中的

$.post("http://test.mw.com/json2.php",{}, function(data){

改成

$.getJSON("http://test.mw.com/json2.php?format=json&jsoncallback=?",{}, function(data){

第二将json2.php最后的

echo$article_json;

改成

echo$_REQUEST['jsoncallback'] . "(" . $article_json . ")";

修改这二个地方后就可以解决JSON的跨域问题了。

下面也给出完整的示例程序,分为json2.php和json2.html, json2.html上有个按钮,按下后将发送AJAX请求得到json2.php返回的跨域JSON数据。注意和上一篇《JSON进阶第二篇 AJAX方式传递JSON数据》文中的代码进行比较。

1.Json2.php

[php] view plaincopyprint?
  1. <?php 
  2. // by MoreWindows( http://blog.csdn.net/MoreWindows ) 
  3. $article_array = array
  4.     array
  5.         "id"=>"001"
  6.         "title"=>"PHP访问MySql数据库 初级篇",  
  7.         "link"=>"http://blog.csdn.net/morewindows/article/details/7102362" 
  8.     ), 
  9.     array
  10.         "id"=>"001"
  11.         "title"=>"PHP访问MySql数据库 中级篇 Smarty技术",  
  12.         "link"=>"http://blog.csdn.net/morewindows/article/details/7094642" 
  13.     ), 
  14.     array
  15.         "id"=>"001"
  16.         "title"=>"PHP访问MySql数据库 高级篇 AJAX技术",  
  17.         "link"=>"http://blog.csdn.net/morewindows/article/details/7086524" 
  18.     ), 
  19. ); 
  20. $article_json = json_encode($article_array); 
  21.  
  22. //echo $article_json; 
  23. echo $_REQUEST['jsoncallback'] ."(" . $article_json .")"
  24. ?> 

2.Json2.html    // by MoreWindows( http://blog.csdn.net/MoreWindows )

[html] view plaincopyprint?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <title>ajax方式请求跨域的json数据</title> 
  5. <scripttype="text/javascript"src="../jquery-1.7.min.js"></script> 
  6. <script type="text/javascript"> 
  7. //显示提示 
  8. function OnMouseEnterDivInfo(thisObj, title) 
  9.     $("#article_link").css("position","absolute"); 
  10.     $("#article_link").css("left","20px"); 
  11.     $("#article_link").css("top",$(thisObj).offset().top + $(thisObj).height()); 
  12.     $("#article_link").html("链接地址" + title); 
  13.     $("#article_link").slideDown("fast"); 
  14.     $(thisObj).css("background-color","red");    
  15. //隐藏提示 
  16. function OnMouseLeaveDivInfo(thisObj) 
  17.     $("#article_link").hide(); 
  18.     $(thisObj).css("background-color","yellow"); 
  19. }   
  20. //jquery通过AJAX方式得到JSON数据 
  21. $(document).ready(function(){ 
  22.     $("#GetDataBtn").click(function(){ 
  23.         //$.post("http://test.mw.com/json2.php", {}, function(data){   
  24.         $.getJSON("http://test.mw.com/json2.php?format=json&jsoncallback=?", {}, function(data){   
  25.             var g_jsonstr = eval(data); 
  26.             var ilen =g_jsonstr.length; 
  27.             var detailhtml = ""
  28.             for (var i =0; i <ilen; i++) 
  29.             { 
  30.                 var divhtml ='<div id=\"div_' + i + '\"  onmouseenter=\"OnMouseEnterDivInfo(this, \' '+ g_jsonstr[i]['link'] + '\');\"onmouseleave=\"OnMouseLeaveDivInfo(this);\">'; 
  31.                 divhtml += '<h1>' + g_jsonstr[i]['title'] + '</h1>'; 
  32.                 divhtml += '</div>'; 
  33.                 detailhtml += divhtml; 
  34.             } 
  35.             $("#detail").html(detailhtml);//生成新的标题区域 
  36.             $("#detail").slideDown("slow"); 
  37.         }); 
  38.     }); 
  39. }); 
  40. </script> 
  41. <style type="text/css"> 
  42. div 
  43.     font-family:sans-serif; 
  44. </style> 
  45. </head>    
  46. <body> 
  47. <input type="button"id="GetDataBtn"value="生成数据"/> 
  48. <divid="detail"> 
  49. </div> 
  50. <p><spanid="article_link"style="display:none;z-index:100"></span></p> 
  51. </body> 
  52. </html> 

再用Firefox的Firebug查看点击按钮后AJAX响应的详细过程,可以发现已经可以收到跨域后的JSON数据了。

有兴趣的筒子可以再试试这种方式:

将http://test.mw.com/json2.php的

echo$_REQUEST['jsoncallback'] . "(" . $article_json . ")";

换成

echo"ShowData($article_json)";

再将json2.html的

$(document).ready(function(){... }

换成

[javascript] view plaincopyprint?
  1. function ShowData(data) 
  2.     var g_jsonstr = eval(data); 
  3.     var ilen = g_jsonstr.length; 
  4.     var detailhtml = ""
  5.     for (var i = 0; i < ilen; i++) 
  6.     { 
  7.         var divhtml = '<div id=\"div_' + i +'\"  onmouseenter=\"OnMouseEnterDivInfo(this, \' '+ g_jsonstr[i]['link'] +'\');\" onmouseleave=\"OnMouseLeaveDivInfo(this);\" >'
  8.         divhtml += '<h1>' + g_jsonstr[i]['title'] +'</h1>'
  9.         divhtml += '</div>'
  10.         detailhtml += divhtml; 
  11.     } 
  12.     $("#detail").html(detailhtml);//生成新的标题区域 
  13.     $("#detail").slideDown("slow"); 
  14. //jquery通过AJAX方式得到JSON数据 
  15. $(document).ready(function(){ 
  16.     $("#GetDataBtn").click(function(){ 
  17.         //jsonp原理--利用<script>标签的开放策略 
  18.         var script = document.createElement('script'); 
  19.         script.setAttribute('src',"http://test.mw.com/json2.php"); 
  20.         document.getElementsByTagName('head')[0].appendChild(script); 

来试试,同样可以得到数据的(有一点不足——此时鼠标移动到标题是不会触发提示的)。

另外,关于JSON有个小小的问题要注意下:

[php] view plaincopyprint?
  1. $array1 = array("a","b"); 
  2. $array2 = array('001'=>"a",'002'=>"b"); 
  3. $json1 = json_encode($array1); 
  4. $json2 = json_encode($array2); 
  5. echo $json1 ."<br />"
  6. echo $json2 ."<br />"

会输出

["a","b"]

{"001":"a","002":"b"}

这说明,$json1是数组,而且$json2是对象。将$json1和$json2传到js代码中,$json1.length是合法的,但$json2.length是未定义的。读者可以对比本篇json2.php中数组$article_array与上一篇的区别。

至此,JSON进阶三篇全部完成。下面列出目录,以供参考:

1.《JSON进阶第一篇 在PHP与javascript 中使用JSON

2.《JSON进阶第二篇 AJAX方式传递JSON数据

3.《JSON进阶第三篇 apache多域名及JSON的跨域问题(JSONP)》

转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/7235992

0 0
原创粉丝点击