`

用Script绕行动态注入解决跨域的问题

阅读更多
<script>
/****************script动态注入*********************************/
function JSONscriptRequest(fullUrl) {
       this.fullUrl = fullUrl;
       this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
       this.headLoc = document.getElementsByTagName("head").item(0);
       this.scriptId = 'JscriptId' +   
       JSONscriptRequest.scriptCounter++;
}
   JSONscriptRequest.scriptCounter = 3;
JSONscriptRequest.prototype.buildScriptTag = function () {

     this.scriptObj = document.createElement("script");
     this.scriptObj.setAttribute("type", "text/javascript");
     this.scriptObj.setAttribute("charset", "gbk");
     this.scriptObj.setAttribute("src", this.fullUrl +  
     this.noCacheIE);
     this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function () {
        this.headLoc.removeChild(this.scriptObj); 
}
JSONscriptRequest.prototype.addScriptTag = function () {
        this.headLoc.appendChild(this.scriptObj);

</script>
<script>
/***********************调用方法******************************/
function mydo(){
  var req = "http://"+IP_PORT+"server?callback=mydodeal&action=getMes&username=username";
         bObj = new JSONscriptRequest(req);
         bObj.buildScriptTag();
         bObj.addScriptTag();

}
/***********************回调函数******************************/
function mydodeal(mytext){
alert(mytext);   //回调的定义变量
}
</script>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics