DOM遍历和同源策略

时间:2017-11-03 21:30:06

标签: javascript jquery html dom iframe

我目前正在研究访问它的DOM节点。然后我遇到了iframe,我尝试访问节点但我一直未定义.ifram受同一源策略保护所以我试过使用CORS,得到一个响应,然后导航它,但我一直未定义........     $ body = document.body;

$body.children[3];
$yo = $body.children[3];
$yo.children[0];
$next = $yo.children[0];
$next.children[3];
$plus = $next.children[3];
$plus.children[0];
$star = $plus.children[0];
$star.children[0];
$staragain = $star.children[0];
$staragain.children[1];
$nextstar = $staragain.children[1];
$nextstar.children[1];
$afternext = $nextstar.children[1]; 

//访问DOM到iframe

var CORSObject = null;
if(typeof(XMLHttpRequest) != "undefined")
{
CORSObject = new XMLHttpRequest();
CORSObject.withCredentials = true;
}
else if(typeof(XDomainRequest) != "undefined")
{
//For IE XDomainRequest is required for CORS request
CORSObject = new XDomainRequest();
//XDomainRequest doesn't support sending and receiving of cookies.
}
else
{
CORSObject = null;
}

if(CORSObject != null)
{
/*
    All browsers set Origin Header while sending an AJAX Request
    Some don't include this header if the AJAX request is for same
domain.
*/

CORSObject.open("GET", "https://www.com/ajaxData.php");

CORSObject.onload = function() {alert(CORSObject.responseText);};
CORSObject.onerror = function() {alert("There was an error")};

CORSObject.send();
}
//process cors response
CORSObject.onload = function() {
var responseText = CORSObject.responseText;
console.log(responseText);
// process the response.
};

CORSObject.onerror = function() {
console.log('There was an error!');
};
//to bypass same origin policy

然后我再次运行代码,但iframe一直在返回undefined .....

如何解决这个问题,访问iframe?

0 个答案:

没有答案
相关问题