zoukankan
html css js c++ java
Ajax跨域访问的问题?
最近在项目中遇到了一个Ajax跨域访问的问题,搞了很长时间还是未能完全解决掉,可有高手能指点一二
我查询资料,基本可有两种实现方式来解决,一种是 在 请求页中加入一个隐藏帧,并在隐藏帧内嵌入一个
想要跨域访问站点的页,这个就要求远程站点上要有可以提供给外部使用的页,通过此页来对本站的访问,
并通过隐藏帧技术将去回来的数据,获取到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
>
//
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程序,大家还有啥更好的办法吗?
查看全文
相关阅读:
剑指offer-包含min函数的栈-栈和队列-python
计算机系统基础(一):程序的表示、转换与链接(第二周小测验)
计算机系统基础(一):程序的表示、转换与链接(第一周小测验)
数据结构与算法(周测2-AVL树)
数据结构与算法(周测1-算法分析)
数据结构-排序
数据结构-图的遍历
数据结构-图基础
数据结构-散列查找
数据结构-堆(优先队列)
原文地址:https://www.cnblogs.com/adam/p/1212314.html
最新文章
激活office 365 的启动文件
【C++】朝花夕拾——树(开篇)
【C++】朝花夕拾——中缀转后缀
【CImg】三角形绘制算法实现
【CImg】基本像素操作
【OpenCV】立体匹配算法SSD、NCC、ASW的基础实现
[leetcode]Add Two Numbers——JS实现
【笔记JS/HTML/CSS】CSS3实现鼠标滑动显示动画(transition、transform)
【OpenCV】像素操作的数字图像处理
【笔记JS/HTML/CSS】web中的HTTP协议(1)
热门文章
【笔记JS/HTML/CSS】ubuntu环境下的sublime text2 安装 zenCoding
剑指offer-二进制中1的个数-进制转化-补码反码原码-python
剑指offer-数值的整数次方-调整数组顺序使奇数位于偶数前面-代码的完整性-python
剑指offer-递归和循环-python
剑指offer-旋转数组的最小数字-数组-python
剑指offer-链表中环的入口结点-链表-python ***
剑指offer-删除链表中重复的结点-链表-python ***
剑指offer-两个链表的第一个公共结点-链表-python
剑指offer-复杂链表的复制-链表-python
剑指offer-栈的压入、弹出序列-栈和队列-python
Copyright © 2011-2022 走看看