JS或JQuery获取页面数据的跨域问题
JS或JQuery获取页面数据存在着跨域问题。
比如要用JQuery 如果跨域获取数据 只能获取 返回json格式 的数据
1、使用$.getJSON方法,可以跨域获取json格式的数据
2、使用$.ajax方法,并且设置type为GET,dataType为jsonp
$.ajax 是没法直接 获取 远程网页源代码,因为存在跨域问题。
你可以这么做:
本地新增一个服务端的页面:
如 ServerGetData.asp, ServerGetData.aspx, ServerGetData.php, ServerGetData.jsp ...
让服务端页面获取数据(他们可以获取远程网页源代码)
比如php代码:
$file = file_get_contents('http://www.baidu.com/');
echo($file);
然后 用 $.ajax 或 $.get 即可
曾想到iframe引入数据再用js获取,可调试的时候同样发现避免不了跨域问题。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<html> <head> <meta http-equiv="contentType" content="text/html;charset=utf-8"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> <script> $(function(){ /* var iframe = document.createElement("iframe"); iframe.src = "http://www.baidu.com/index.php"; if (iframe.attachEvent){ iframe.attachEvent("onload", function(){ alert("loaded"); });} else { iframe.onload = function(){ alert("loaded"); }; } */ var if_w = $("body").width(); var if_h = $(window).height(); //var if_w = 800, if_h = 800; //$("<iframe width='" + if_w + "' height='" + if_h + "' id='iframe1' style='z-index:-1'></iframe>").prependTo('body'); $("<iframe width='" + if_w + "' height='" + if_h + "' id='iframe1' style='display:none;z-index:-1'></iframe>").appendTo('body'); $("#phone").change(function(){ $("#message").text("正在加载.."); $("#iframe1").attr("src",$(this).val()); }); $("#iframe1").load(function(){ $("#message").text("载入完毕"); //alert($("#iframe1").contents().html()); //alert($(document.getElementById('iframe1').contentWindow.document.body).html()); //alert($(window.frames["iframe1"].document).html()); var win = document.getElementById('iframe1').contentWindow; var html = win.document.body.innerHTML; alert(html); }); }); </script> </head> <body> <select id="phone"> <option value="about:blank">请选择</option> <option value="http://www.baidu.com/">baidu</option> <option value="http://www.sogou.com/">sogou</option> <option value="http://www.youdao.com/">youdao</option> </select> <span id="message" style="color:green"></span> <hr/> <!-- <iframe id="iframe1" src="" width="600" height="700"></iframe> --> </body> </html>
JS获取整个页面文档的实现代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <div id="test"> <pre> 休息休息 </pre> </div> <script type="text/javascript" src="ajax.js"></script> <script type="text/javascript"> var connector = (function(){ var connector = null; if(window.XMLHttpRequest){ connector = new XMLHttpRequest(); }else if(window.ActiveXObject){ connector = new ActiveXObject('Microsoft.XMLHTTP'); } return connector; })(); var innerText = document.body.innerText ? 'innerText' : 'textContent'; var handler = function(response){ document.getElementById('test').getElementsByTagName('pre')[0][innerText] = response; } connector.onreadystatechange = (function(callback){ return function(){ if(connector.readyState == 4){//这里connector.status == 200都省了。 callback.call(connector,connector.responseText); } } })(handler); connector.open('GET','aa.html',true);//发送到本页面 connector.send(); </script> </body> </html>
远程网页源代码读取:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>远程网页源代码读取</title> <style type="text/css"> /* 页面字体样式 */ body, td, input, textarea { font-family:Arial; font-size:12px; } </style> <script type="text/javascript"> //用于创建XMLHttpRequest对象 function createXmlHttp() { //根据window.XMLHttpRequest对象是否存在使用不同的创建方式 if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); //FireFox、Opera等浏览器支持的创建方式 } else { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器支持的创建方式 } } //直接通过XMLHttpRequest对象获取远程网页源代码 function getSource() { var url = document.getElementById("url").value; //获取目标地址信息 //地址为空时提示用户输入 if (url == "") { alert("请输入网页地址。"); return; } document.getElementById("source").value = "正在加载……"; //提示正在加载 createXmlHttp(); //创建XMLHttpRequest对象 xmlHttp.onreadystatechange = writeSource; //设置回调函数 xmlHttp.open("GET", url, true); xmlHttp.send(null); } //将远程网页源代码写入页面文字区域 function writeSource() { if (xmlHttp.readyState == 4) { document.getElementById("source").value = xmlHttp.responseText; } } </script> </head> <body> <h1>远程网页源代码读取</h1> <div> 地址:<input type="text" id="url"> <input type="button" onclick="getSource()" value="获取源码"> </div> <textarea rows="10" cols="80" id="source"></textarea> </body> </html>