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程序,大家还有啥更好的办法吗?
查看全文
相关阅读:
微信小程序入门实例
textarea 的value值以及演示
Think php (TP5) 批量删除全部源码
Think php TP5 CURD 增删改查全部源码
TP5 Think php 批量添加全部源码
centos7 supervisor管理redis
Centos7 设置redis开机自启
Centos7安装gearman和php扩展
nginx-403
FFmpeg-截取视频图片
原文地址:https://www.cnblogs.com/adam/p/1212314.html
最新文章
upc组队赛1 小C的数学问题【单调栈】(POJ2796)
upc组队赛3 Congestion Charging Zon【模拟】
upc组队赛3 Chaarshanbegaan at Cafebazaar
upc组队赛3 Iranian ChamPions Cup
HDU 1849 Rabbit and Grass 【Nim博弈】
HDU 1848 Fibonacci again and again【博弈SG】
POJ 1751 Highways 【最小生成树 Kruskal】
upc组队赛2 Super-palindrome【暴力枚举】
upc组队赛2 Hakase and Nano【思维博弈】
upc组队赛2 Master of GCD 【线段树区间更新 || 差分】
热门文章
007.NET5 Log4Net组件使用
[转]轻松搞定权限设计1-页面按钮控制(精确到某个用户和某个按钮)
asp.net 从Excel表导入数据到数据库中
[转]C# web 读取Excel文件
【珍藏】 2012.NET开发必看资料53个+经典源码77个—下载目录
SQLite在C#的使用
视频监控资源整理
编写高质量代码:改善C#程序的157个建议
Centos7安装php7的redis[]
Centos7上yum安装配置LNMP环境 [阿里云服务器][]
Copyright © 2011-2022 走看看