跨子域post 提交

最近易迅有签到活动,看了下代码,发现有相关跨域的部分,稍微整理了下,备注下

from 和 iframe 相互结合进行的,也设置了相关 document.domain

个人感觉没必要,还是首推jsonp方式,只是没办法支持post提交

js

G.util.post = function(c, g, b) {
    G.util.post.pIndex = (G.util.post.pIndex || 0) + 1;
    var d = $('<iframe name="pIframe_' + G.util.post.pIndex + '" src="about:blank" style="display:none" width="0" height="0" scrolling="no" allowtransparency="true" frameborder="0"></iframe>').appendTo($(document.body));
    var a = [];
    $.each(g, function(i, h) {
        a.push('<input type="hidden" name="' + i + '" value="" />')
    });
    if (!/(\?|&(amp;)?)fmt=[^0 &]+/.test(c)) {
        c += (c.indexOf("?") > 0 ? "&" : "?") + "fmt=1"
    }
    var f = $('<form action="' + c + '" method="post" target="pIframe_' + G.util.post.pIndex + '">' + a.join("") + "</form>").appendTo($(document.body));
    $.each(g, function(i, h) {
        f.children("[name=" + i + "]").val(h)
    });
    d[0].callback = function(h) {
        if (typeof b == "function") {
            b(h)
        }
        $(this).src = "about:blank";
        $(this).remove();
        f.remove();
        d = f = null
    };
    if ($.browser.msie && $.browser.version == 6) {
        d[0].pIndex = G.util.post.pIndex;
        d[0].ie6callback = function() {
            f.target = "pIframe_" + this.pIndex;
            f.submit()
        };
        d[0].src = location.protocol + "//st.51buy.com/static_v1/htm/ie6post.htm"
    } else {
        f.submit()
    }
}; 
st.51buy.com/static_v1/htm/ie6post.htm
<!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" xml:lang="zh-cn" lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta http-equiv="Content-Language" content="zh-cn" />
    <title>POST</title>
    <script type="text/javascript">document.domain='51buy.com';</script>
</head>
<body>
    <script type="text/javascript">
    <!--
        if('function' == typeof frameElement.ie6callback) frameElement.ie6callback();
    //-->
    </script>
</body>
</html>

返回的信息

<?xml version="1.0" encoding="gb2312"?>
<!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" xml:lang="zh-cn" lang="zh-cn">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta http-equiv="Content-Language" content="gb2312" />
    <meta name="robots" content="all" />
    <meta name="author" content="Tencent-ISRD" />
    <meta name="Copyright" content="Tencent" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <title>POST - ICSON</title>
</head>
<body>
<script type="text/javascript">
<!--
    document.domain="51buy.com";
    frameElement.callback({"errno":0,"data":{"signinfo":[{"sign_time":"2013-05-04 08:09:22","award_id":"7|11|"},{"sign_time":"2013-05-11 22:27:00"},{"sign_time":"2013-05-13 23:04:57"},{"sign_time":"2013-05-14 22:23:27"},{"sign_time":"2013-05-15 22:36:46"}],"curtime":1368669984}});
//-->
</script>
</body>
</html>

 

posted @ 2013-05-16 10:27  游云  阅读(326)  评论(0编辑  收藏  举报