Ajax跨域访问的问题?
最近在项目中遇到了一个Ajax跨域访问的问题,搞了很长时间还是未能完全解决掉,可有高手能指点一二
我查询资料,基本可有两种实现方式来解决,一种是 在 请求页中加入一个隐藏帧,并在隐藏帧内嵌入一个
想要跨域访问站点的页,这个就要求远程站点上要有可以提供给外部使用的页,通过此页来对本站的访问,
并通过隐藏帧技术将去回来的数据,获取到Parent页上。
第二种方法是在本地做一个远程的代理程序,Ajax想访问本站的代理程序,由代理程序去访问外部页信息 ,
如使用HttpClient或 WebClient进行远程的数据请求,然后反给前台显示。
我查询资料,基本可有两种实现方式来解决,一种是 在 请求页中加入一个隐藏帧,并在隐藏帧内嵌入一个
想要跨域访问站点的页,这个就要求远程站点上要有可以提供给外部使用的页,通过此页来对本站的访问,
并通过隐藏帧技术将去回来的数据,获取到Parent页上。
第二种方法是在本地做一个远程的代理程序,Ajax想访问本站的代理程序,由代理程序去访问外部页信息 ,
如使用HttpClient或 WebClient进行远程的数据请求,然后反给前台显示。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<link rel="stylesheet" type="text/css" href="css/style_0704.css" />
<link rel="stylesheet" type="text/css" href="css/zhinan.css" />
</head>
<body>
<center>
<script language="javascript" src="http://resource.elong.com/js/myelong/cn/nav.js"></script>
<script language="javascript" src="Ajax.js"></script>
<script language="javascript">
<!--
var str_url;
str_url = window.location.href;
document.write("<link rel=\"stylesheet\" type=\"text\/css\" href=\"http:\/\/resource.elong.com\/css\/index_0601\/cn\/common.css\">");
document.write("<div id=\"top\">");
document.write(" <div class=\"logo\"><a href=\"http:\/\/www.elong.com\"><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/logo.gif\" width=\"279\" height=\"75\" border=\"0\" alt=\"酒店预订 机票预订 - 艺龙旅行网\" \/><\/a><\/div>");
document.write("<div class=\"banner\">");
doRequest("http://elong.allyes.com/main/adfshow?user=Elong|air|banner_468x60&db=elong&border=0&local=yes&js=ie");
var str_Ad = doCallback();
if (str_Ad != null && str_Ad != "") eval(str_Ad);
document.write("<\/div>");
//document.write("<scr" + "ipt type=\"text\/javascript\" src=\"http:\/\/elong.allyes.com\/main\/adfshow?user=Elong|travelguide|banner_468x60&db=elong&border=0&local=yes&js=ie\"><\/scr" + "ipt><\/div>");
document.write("<\/div>");
document.write("<\/div>");
document.write("<div id=\"nav\">");
document.write(" <ul>");
document.write(" <li class=\"w1 nav_link\"><a href=\"http:\/\/www.elong.com\">首页<\/a><\/li>");
document.write(" <li class=\"w2 nav_link\"><a href=\"http:\/\/www.elong.com\/hotels\/\">国内酒店<\/a><\/li>");
document.write(" <li class=\"w3 nav_link\"><a href=\"http:\/\/www.elong.com\/hotels\/default2.aspx\">港澳台酒店<\/a><\/li>");
document.write(" <li class=\"w4 nav_link\"><a href=\"http:\/\/www.elong.com\/globalhotels\/\">海外酒店<\/a><\/li>");
document.write(" <li class=\"w5 nav_link\"><a href=\"http:\/\/www.elong.com\/flights\/\">国内机票<\/a><\/li>");
document.write(" <li class=\"w6 nav_link\"><a href=\"http:\/\/www.elong.com\/flights\/default2.aspx\">国际机票<\/a><\/li>");
document.write(" <li class=\"w7b nav_linkb\"><a href=\"http:\/\/zhinan.elong.com\">目的地指南<\/a><\/li>");
document.write(" <li class=\"w8 nav_link\"><a href=\"http:\/\/special.elong.com\">特约商户<\/a><\/li>");
document.write(" <li class=\"w9 nav_link\"><a href=\"http:\/\/www.elong.com\/square\/store.asp\">积分广场<\/a><\/li>");
document.write(" <\/ul>");
document.write(" <div class=\"elong_cn_big5\">");
if(str_url.toLowerCase().indexOf("big5.elong") >0)
{
document.write(" <a href=\"ht" + "tp:\/\/www.elong.net\" class=\"imgr\" ><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_en.gif\" width=\"42\" height=\"13\" border=\"0\" alt=\"English\"\/><\/a><a href=\"ht" + "tp:\/\/www.elong.com\"><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_cn.gif\" width=\"48\" height=\"13\" border=\"0\" alt=\"简体中文\" \/><\/a>");
}
else
{
document.write(" <a href=\"http:\/\/www.elong.net\" class=\"imgr\" ><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_en.gif\" width=\"42\" height=\"13\" border=\"0\" alt=\"English\"\/><\/a><a href=\"http:\/\/big5.elong.com\"><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_big5.gif\" width=\"48\" height=\"13\" border=\"0\" alt=\"繁体中文\" \/><\/a>");
}
document.write(" <\/div>");
document.write("<\/div>");
-->
</SCRIPT>
<script type="text/javascript" src="http://www.elong.com/loginmenu.js" charset="GB2312"></script>
<script type='text/javascript' src='http://elong.allyes.com/main/adfshow?user=Elong|air|banner_468x60&db=elong&border=0&local=yes&js=ie'></script>
<h1>abcd</h1>
</center>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<link rel="stylesheet" type="text/css" href="css/style_0704.css" />
<link rel="stylesheet" type="text/css" href="css/zhinan.css" />
</head>
<body>
<center>
<script language="javascript" src="http://resource.elong.com/js/myelong/cn/nav.js"></script>
<script language="javascript" src="Ajax.js"></script>
<script language="javascript">
<!--
var str_url;
str_url = window.location.href;
document.write("<link rel=\"stylesheet\" type=\"text\/css\" href=\"http:\/\/resource.elong.com\/css\/index_0601\/cn\/common.css\">");
document.write("<div id=\"top\">");
document.write(" <div class=\"logo\"><a href=\"http:\/\/www.elong.com\"><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/logo.gif\" width=\"279\" height=\"75\" border=\"0\" alt=\"酒店预订 机票预订 - 艺龙旅行网\" \/><\/a><\/div>");
document.write("<div class=\"banner\">");
doRequest("http://elong.allyes.com/main/adfshow?user=Elong|air|banner_468x60&db=elong&border=0&local=yes&js=ie");
var str_Ad = doCallback();
if (str_Ad != null && str_Ad != "") eval(str_Ad);
document.write("<\/div>");
//document.write("<scr" + "ipt type=\"text\/javascript\" src=\"http:\/\/elong.allyes.com\/main\/adfshow?user=Elong|travelguide|banner_468x60&db=elong&border=0&local=yes&js=ie\"><\/scr" + "ipt><\/div>");
document.write("<\/div>");
document.write("<\/div>");
document.write("<div id=\"nav\">");
document.write(" <ul>");
document.write(" <li class=\"w1 nav_link\"><a href=\"http:\/\/www.elong.com\">首页<\/a><\/li>");
document.write(" <li class=\"w2 nav_link\"><a href=\"http:\/\/www.elong.com\/hotels\/\">国内酒店<\/a><\/li>");
document.write(" <li class=\"w3 nav_link\"><a href=\"http:\/\/www.elong.com\/hotels\/default2.aspx\">港澳台酒店<\/a><\/li>");
document.write(" <li class=\"w4 nav_link\"><a href=\"http:\/\/www.elong.com\/globalhotels\/\">海外酒店<\/a><\/li>");
document.write(" <li class=\"w5 nav_link\"><a href=\"http:\/\/www.elong.com\/flights\/\">国内机票<\/a><\/li>");
document.write(" <li class=\"w6 nav_link\"><a href=\"http:\/\/www.elong.com\/flights\/default2.aspx\">国际机票<\/a><\/li>");
document.write(" <li class=\"w7b nav_linkb\"><a href=\"http:\/\/zhinan.elong.com\">目的地指南<\/a><\/li>");
document.write(" <li class=\"w8 nav_link\"><a href=\"http:\/\/special.elong.com\">特约商户<\/a><\/li>");
document.write(" <li class=\"w9 nav_link\"><a href=\"http:\/\/www.elong.com\/square\/store.asp\">积分广场<\/a><\/li>");
document.write(" <\/ul>");
document.write(" <div class=\"elong_cn_big5\">");
if(str_url.toLowerCase().indexOf("big5.elong") >0)
{
document.write(" <a href=\"ht" + "tp:\/\/www.elong.net\" class=\"imgr\" ><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_en.gif\" width=\"42\" height=\"13\" border=\"0\" alt=\"English\"\/><\/a><a href=\"ht" + "tp:\/\/www.elong.com\"><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_cn.gif\" width=\"48\" height=\"13\" border=\"0\" alt=\"简体中文\" \/><\/a>");
}
else
{
document.write(" <a href=\"http:\/\/www.elong.net\" class=\"imgr\" ><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_en.gif\" width=\"42\" height=\"13\" border=\"0\" alt=\"English\"\/><\/a><a href=\"http:\/\/big5.elong.com\"><img src=\"http:\/\/resource.elong.com\/images\/index_0601\/cn\/elong_big5.gif\" width=\"48\" height=\"13\" border=\"0\" alt=\"繁体中文\" \/><\/a>");
}
document.write(" <\/div>");
document.write("<\/div>");
-->
</SCRIPT>
<script type="text/javascript" src="http://www.elong.com/loginmenu.js" charset="GB2312"></script>
<script type='text/javascript' src='http://elong.allyes.com/main/adfshow?user=Elong|air|banner_468x60&db=elong&border=0&local=yes&js=ie'></script>
<h1>abcd</h1>
</center>
</body>
</html>
//Defined Ajax HttpRequest.
var xmlHttp = null;
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
//do Request.
function doRequest(sUrl)
{
//处理Firefox 安全问题
if (window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e)
{
alert("Permission UniversalBrowserRead denied.");
}
}
//true:异步;false:同步;
xmlHttp.open("GET",sUrl,false);
xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = doCallback;
xmlHttp.send(null);
}
//do Callback.
function doCallback()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var ResText = xmlHttp.responseText;
//return eval("(" + ResText + ")");
return ResText;
}
}
}
上面是我出问题的Demo程序,大家还有啥更好的办法吗?var xmlHttp = null;
if (window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
//do Request.
function doRequest(sUrl)
{
//处理Firefox 安全问题
if (window.netscape)
{
try
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e)
{
alert("Permission UniversalBrowserRead denied.");
}
}
//true:异步;false:同步;
xmlHttp.open("GET",sUrl,false);
xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = doCallback;
xmlHttp.send(null);
}
//do Callback.
function doCallback()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
var ResText = xmlHttp.responseText;
//return eval("(" + ResText + ")");
return ResText;
}
}
}