1. 程式人生 > >利用flash讓IE7.8.9ajax支援跨域

利用flash讓IE7.8.9ajax支援跨域

利用flash讓IE7.8.9ajax支援跨域

廢話不多說,直接上核心呼叫程式碼

<script>
/**
 *  // 解決IE8下ajax無法跨域的問題 $.flashAjax
    $.flashAjax({
        flash_id: 'JS_flashProductBooking',
        data_type: "text", //text 、json,
        page_charset: 'gb2312', //gb2312 、utf-8,
        debug: false,
        url: ajxa_url,
        data: '',
        type: 'GET',
        success: function (data) {
            // todo...
        }
    });
 */
$.extend({
    flashAjax: function (setting) {
        var JS_flashAjax = null;
        var userAgent = window.navigator.userAgent.toLowerCase();
        if(userAgent.indexOf("msie 9.0")>0 || userAgent.indexOf("msie 8.0")>0 || userAgent.indexOf("msie 7.0")>0) {
            if(typeof window.SWFLwkPost == 'undefined') {
                loadSWFPost(initSWFPost);
            }else {
                initSWFPost();
            }
            function initSWFPost() {
                try {/*在低版本IE下*/
                    /*var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");不報錯表示支援flash*/
                    var _flashBtn = document.createElement('div');
                    _flashBtn.id = setting.flash_id || '_flashAjaxBtn';
                    _flashBtn.style.display="none";
                    document.getElementsByTagName('body')[0].appendChild(_flashBtn);
                    JS_flashAjax = new SWFLwkPost({
                        button_height:1,
                        button_width:1,
                        data_type:setting.data_type || 'json', /*text、json*/
                        page_charset:setting.page_charset || 'utf-8',/*gb2312、utf-8*/
                        /*開啟除錯開關*/
                        debug: setting.debug || false,
                        /*flash地址*/
                        flash_url: "/ext/swflwkpost/swflwkpost.swf",
                        /*請求URL地址*/
                        upload_url:setting.url || '',
                        /*FLASH 載入位置ID*/
                        button_placeholder_id: setting.flash_id || '_flashAjaxBtn',
                        upload_complete_handler: function (data) {
                            if(setting.success){
                                setting.success(data);
                            }
                        },
                        flashready:false
                    });
                    setTimeout(function () {
                        JS_flashAjax.postAddParamData((setting.data || ''));/*傳表單序列化字串物件*/
                        JS_flashAjax.postSend();
                    },400);
                } catch (e) {/*低版本IE且不支援flash*/
                }
            }
            function loadSWFPost(callback) {
                var swfpost = document.createElement("script");
                swfpost.type = "text/javascript";
                swfpost.charset = "utf-8";
                swfpost.src = '/ext/swflwkpost/swflwkpost.js';
                if(typeof swfpost.onload == 'undefined') {
                    swfpost.onreadystatechange = function () {
                        var r = swfpost.readyState;
                        if (r === 'loaded' || r === 'complete') {
                            swfpost.onreadystatechange = null;
                            callback();
                        }
                    };
                } else {
                    swfpost.onload = function(){
                        callback();
                    }
                }
                var head = document.getElementsByTagName('head')[0];
                var s = head.getElementsByTagName("script")[0];
                if(s == undefined) {
                    if (head != undefined) {
                        head.appendChild(swfpost);
                    }
                } else {
                    s.parentNode.appendChild(swfpost);
                }
            }
        }else {
            $.ajax(setting);
        }
    }
});
</script>

依賴檔案(/ext/swflwkpost/*)

資原始檔下載: https://download.csdn.net/download/liguanjie8/10874719