国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

PHP ajax跨子域的解決方案之document.domain+iframe實例分析

瀏覽:102日期:2022-09-11 10:38:46

本文實例講述了PHP ajax跨子域的解決方案之document.domain+iframe。分享給大家供大家參考,具體如下:

對于主域相同,子域不同,我們可以設置相同的document.domain來欺騙瀏覽器,達到跨子域的效果。

例如:我們有兩個域名:www.a.com 和 img.a.com

在www.a.com下有a.html

在img.a.com下有img.json和img.html這兩個文件。

img.json就是一些我們要獲取的數(shù)據(jù):

[ { 'name' : 'img1', 'url' : 'http://img.a.com/img1.jpg' }, { 'name' : 'img2', 'url' : 'http://img.a.com/img2.jpg' }]

img.html就是我們iframe要引用的:

<!DOCTYPE html><html><head> <meta charset='UTF-8'> <title>Insert title here</title></head><body><script src='http://www.intensediesel.com/bcjs/jquery.js'></script><script type='text/javascript'> document.domain = 'a.com'; var p = parent.window.$; p('#sub').text('我是子頁面添加的');</script></body></html>

a.html就是要通過跨子域獲取數(shù)據(jù)的頁面:

<!DOCTYPE html><html><head> <meta charset='UTF-8'> <title>Insert title here</title></head><body><!-- 通過跨域獲取數(shù)據(jù),并添加到ul中 --><ul id='data'></ul><!-- 子頁面通過parent.window來訪問父頁面 --><div id='sub'></div><!-- 通過iframe引用img.a.com下的img.html --><iframe src='http://img.a.com/img.html'></iframe><script src='http://www.intensediesel.com/bcjs/jquery.js'></script><script type='text/javascript'>document.domain = 'a.com';$('#iframe').bind('load', function() { //獲取子頁面的jquery對象 iframe = document.getElementById('iframe').contentWindow.$; iframe.getJSON('http://img.a.com/img.json', function(data) { var con = ''; //注意這里的$對象是www.a.com上的 $.each(data, function(i, v) { con += '<li>' + v.name + ':' + v.url + '</li>'; }); $('#data').html(con); });});</script></body></html>

a.html中我們通過contentWindow.$來獲取子頁面的jquery對象,然后通過getJSON獲取數(shù)據(jù),并通過www.a.com上的$對象把數(shù)據(jù)寫入到ul中。

在子頁面img.html中我們通過parent.window來訪問父頁面的$對象,并操作元素添加數(shù)據(jù)。

更多關于PHP相關內(nèi)容可查看本站專題:《PHP+ajax技巧與應用小結(jié)》、《PHP網(wǎng)絡編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

標簽: PHP
相關文章:
主站蜘蛛池模板: 镇赉县| 绥宁县| 土默特左旗| 垦利县| 天峻县| 沙雅县| 府谷县| 姚安县| 剑阁县| 铁力市| 松阳县| 亚东县| 府谷县| 红安县| 通州市| 武功县| 竹山县| 桃园县| 新宁县| 南充市| 博乐市| 平和县| 南安市| 崇左市| 广安市| 怀来县| 天柱县| 威信县| 来凤县| 清丰县| 娱乐| 张家口市| 大足县| 武义县| 布尔津县| 赤壁市| 炉霍县| 诸城市| 茂名市| 玉龙| 祥云县|