zoukankan      html  css  js  c++  java
  • 跨子域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>
  • 相关阅读:
    [云计算&大数据]概念辨析:数据仓库 | 数据湖 | 数据中心 | 数据中台 | 数据平台 【待续】
    [云计算]概念辨析:云计算 [IaaS/PaaS/SaaS & 公有云/私有云/混合云]
    [Linux]浅析"command > /dev/null 2>&1 &" 与 "command 1>/dev/null 2>&1 &"
    [Python]【Form Data vs Request Payload】之 python 爬虫如何实现 POST request payload 形式的请求
    [Python]PyCharm中出现unresolved reference的解决方法
    [数据库/MYSQL]MYSQL开启Bin-Log
    scrapy采集gb2312网页中文乱码笔记
    uTools 桌面软件。
    Asp.Net Core Grpc 入门实践
    ASP.NET Core 中间件(Middleware)(一)
  • 原文地址:https://www.cnblogs.com/legu/p/3081301.html
Copyright © 2011-2022 走看看