使用iframe来做post方式的跨域访问,通过iframe的onload事件来监听请求是否完成。
原来以为iframe的onload的事件也有跨域的限制,测试了一下,是没有的,测试了IE、火狐、safari都很好使。
1 |
<div contenteditable="false" class="HighLighter"><div contenteditable="false" class="dp-highlighter"><div class="bar"> </div><ol start="1" class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li><li class=""><span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">script</span><span class="tag">></span><span> </span></span></li><li class=""><span> function _onload() { // 注意,不能使用onload(),因为这本身是一个内置函数 </span></li><li class="alt"><span> alert("ok"); </span></li><li class=""><span> } </span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span> </span></span></li><li class=""><span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li><li class=""><span> <span class="tag"><</span><span class="tag-name">iframe</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"http://login.sina.com.cn/"</span><span> </span><span class="attribute">onload</span><span>=</span><span class="attribute-value">"_onload();"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">iframe</span><span class="tag">></span><span> </span></span></li><li class="alt"><span><span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li><li class=""><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span></span></span></li></ol></div><div contenteditable="false" style="display:none" class="xml"><pre><html> <head> <script> function _onload() { // 注意,不能使用onload(),因为这本身是一个内置函数 alert("ok"); } </script> </head> <body> <iframe src="http://login.sina.com.cn/" onload="_onload();"></iframe> </body> </html> |