Ajax跨域访问问题-方法大全

论坛 期权论坛     
选择匿名的用户   2021-5-28 02:18   23   0
<p> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"><strong>Case I. Web代理的方式 (on Server A)<br></strong></p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">即用户访问A网站时所产生的对B网站的跨域访问请求均提交到A网站的指定页面,由该页面代替用户页面完成交互,从而返回合适的结果。此方案可以解决现阶段所能够想到的多数跨域访问问题,但要求A网站提供Web代理的支持,因此A网站与B网站之间必须是紧密协作的,且每次交互过程,A网站的服务器负担增加,且无法代用户保存session状态。</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"><strong>Case II. on-Demand方式 (on Server A)<br></strong></p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">MYMSN的门户就用的这种方式,不过 MYMSN中不涉及跨域访问问题。在页面内动态生成新的&lt;script&gt;,将其src属性指向别的网站的网址,这个网址返回的内容必须是合法的Javascript脚本,常用的是JSON消息。此方案存在的缺陷是, script的src属性完成该调用时采取的方式时get方式,如果请求时传递的字符串过大时,可能会无法正常运行。不过此方案非常适合聚合类门户使用。</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">&lt;html&gt;<br>&lt;head&gt;<br>&lt;script&gt;<br>function loadContent()<br>{<!-- --><br>var s&#61;document.createElement(&#39;SCRIPT&#39;);<br>s.src&#61;&#39;<a href="http://www.anotherdomain.com/TestCrossJS.aspx?f&#61;setDivContent%27;" style="color:#555555;text-decoration:none;"><span style="color:#6466b3;">http://www.anotherdomain.com/TestCrossJS.aspx?f&#61;setDivContent&#39;;</span></a><br>document.body.appendChild(s);<br>}</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">function setDivContent(v)<br>{<!-- --><br>var dv &#61; document.getElementById(&#34;dv&#34;);<br>dv.innerHTML &#61; v; <br>}<br>&lt;/script&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>&lt;div&gt;&lt;/div&gt;</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">&lt;input value&#61;&#34;Click Me&#34;&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">其中的www.anotherdomain.com/TestCrossJS.aspx是这样的,</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">&lt;script runat&#61;&#34;server&#34;&gt;<br>void Page_Load(object sender, EventArgs e)<br>{<!-- --><br>  string f &#61; Request.QueryString[&#34;f&#34;];<br>  Response.Clear();<br>  Response.ContentType &#61; &#34;application/x-javascript&#34;;<br>  Response.Write(String.Format(&#64;&#34;<br>                   {0}(&#39;{1}&#39;);&#34;, <br>                   f,<br>                   DateTime.Now));<br>  Response.End();<br>}<br>&lt;/script&gt;</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">点击“Click Me”按钮,生成一个新的script tag,下载对应的 Javascript 脚本,结束时回调其中的setDivContent(),从而更新网页上一个div的内容。</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"><strong>Case III. iframe方式 (on Server A)<br></strong></p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">查看过醒来在javaeye上的一篇关于跨域访问的帖子,他提到自己已经用iframe的方式解决了跨域访问问题。数据提交跟获取,采用iframe这种方式的确可以了,但由于父窗口与子窗口之间不能交互(跨域访问的情况下,这种交互被拒绝),因此无法完成对父窗口效果的影响。 </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">在页面内嵌或动态生成指向别的网站的IFRAME,然后这2个网页间可以通过改变对方的anchor hash fragment来传输消息。改变一个网页的anchor hash fragment并不会使浏览器重新装载网页,所以一个网页的状态得以保持,而网页本身则可以通过一个计时器(timer)来察觉自己anchor hash的变化,从而相应改变自己的状态。</p>
<p style="font-family:verdana;font-size:14px;line-height:22px;"> </p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">1. <a href="http://domain1/TestCross.html:" style="color:#555555;text-decoration:none;"><span style="color:#6466b3;">http://domain1/TestCross.html:</span></a></p>
<p style="font-family:verdana;font-size:14px;line-height:22px;">&lt;html&gt;<br>&lt;head&gt;<br>&lt;script&gt;<br>var url &#61; &#34;<a href="http://domain2/TestCross.html%22;" style="color:#555555;text-decoration:none;"><span style="color:#6466b3;">http://domain2/TestCross.html&#34;</span></a><br>var
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP