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程序,大家还有啥更好的办法吗?
查看全文
相关阅读:
源码安装mysql-5.7.13一周的冤枉路总结。满满的都是泪啊
一键安装Apache服务脚本
源码编译安装LAMP
Vue侦听器 watch
Vue计算属性 computed
Vue表单的值绑定和修饰符
js编码解码decodeURI(URIstring)与decodeURIComponent(URIstring)的区别
常用的JS表单验证
js正则表达式匹配手机号中间四位以及匹配姓名第一个字符,将其替换为*
Vue按键修饰符,鼠标按钮修饰符
原文地址:https://www.cnblogs.com/adam/p/1212314.html
最新文章
String,StringBuilder,StringBuffer
web渗透,攻防环境搭建
win常见高危漏洞
Linux常见漏洞
msf渗透过程实例
PHP错误处理
php文件系统
php中正则表达式
自启动模块构造-注册表写入
白加黑源码免杀学习
热门文章
2020/12/12-三六零
持久化-Word库加载项劫持
2020/11/30-中泰证券
2020/12/1-四维图新
Office宏病毒学习第一弹--恶意的Excel 4.0宏
转载-通过太阳和投射的阴影进行人员地理位置定位
持久化-Powershell配置文件持久性
笔记-mysqladmin修改密码
shell脚本之拒绝非法用户远程
shell四剑客入门
Copyright © 2011-2022 走看看