创建script标签的方式解决跨域问题 - ZKUI

Nothing is impossible

创建script标签的方式解决跨域问题

//前提接口支持jsonp和指定callback方法
function ajaxReq(urlstring,callback) {
    var scriptBlock = document.createElement("script");
    scriptBlock.src = urlstring;
    //webcti_debug(urlstring);
    scriptBlock.language = "javascript";
    scriptBlock.setAttribute('type','text/javascript');
    document.getElementsByTagName("head")[0].appendChild(scriptBlock);
    scriptBlock.onload = scriptBlock.onreadystatechange = function() {
        if (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete') {
            // callback在此处执行
            scriptBlock.onload = scriptBlock.onreadystatechange = callback();
        }
    };
}

kuayu

分享