玩转C科技.NET

从学会做人开始认识这个世界!http://volnet.github.io

导航

Using the Iframe to implement the Ajax

这篇文档主要解决的问题是:在不使用JavaScript进行跨域访问的前提下,实现post回发,进行表单提交。

vframe_post.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>No freash submit! Post!</title>
</head>
<body>
    <iframe id="_vframe" name="_vframe" style="display:none;"></iframe>
    <form id="formAct" name="formAct" method="post" action="vframe_receive.html">
        <input type="submit" id="btnNormalSubmit" value="Normal Submit" />
    </form>
    <form id="formHidden" name="formHidden" method="post" action="vframe_receive.html" target="_vframe">
        <input type="submit" id="btnHiddenSubmit" value="Hidden Submit" />
    </form>
</body>
</html>

vframe_receive.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>No freash submit! Received!</title>
    <script language="javascript" type="text/javascript">
        function displayMessage() {
            alert("Arrived!");
        }
    </script>
</head>
<body onload="displayMessage();">
    VFrame receive Page!
</body>
</html>

在上例中vframe_post.html中提供了两个form,第一个是普通的form,第二个则是设置了target指向一个iframe。这样做的好处就是在formHidden提交的时候,其数据内容将通过iframe进行提交,而不会刷新当前页面。这一点是Ajax效果的一种体现,但却有别于基于XMLHttpRequest对象的Ajax效果。在一些特定的场合将是非常有用的。各宗滋味,自行体会。

posted on 2009-10-14 01:08  volnet(可以叫我大V)  阅读(376)  评论(0编辑  收藏  举报

使用Live Messenger联系我
关闭