欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js跨域資源共享 基礎(chǔ)篇

 更新時(shí)間:2016年07月02日 11:06:40   作者:光明大神棍  
這篇文章主要為大家詳細(xì)介紹了javascript跨域資源共享的相關(guān)資料,感興趣的朋友可以參考一下

本文詳細(xì)介紹了javascript跨域資源共享,供大家參考,具體內(nèi)容如下

1.為什么提出跨域資源共享(CORS)?
    因?yàn)閄HR實(shí)現(xiàn)ajax的安全限制是:XHR 對(duì)象只能訪問(wèn)與包含它的頁(yè)面位于同一個(gè)域中的資源

2.如何實(shí)現(xiàn)跨域?(跨瀏覽器)

  // 跨瀏覽器創(chuàng)建并返回CORS對(duì)象
  // param method : 請(qǐng)求的方式, get or post
  // param url : 跨域請(qǐng)求的url
  // return xhr : 返回的跨域資源對(duì)象
  function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr){
      xhr.open(method, url, true);  // CORS都是通過(guò)異步的請(qǐng)求
    } else if (typeof XDomainRequest != "undefined"){  // IE
      vxhr = new XDomainRequest();
      xhr.open(method, url);
    } else {
      xhr = null;
    }
    return xhr;
  }
  var request = createCORSRequest("get", "http://localhost/aaa/dome2.php");
  if (request){
    // 用于替代onreadystatechange 檢測(cè)成功,表示接受數(shù)據(jù)完畢
    request.onload = function(){
      // 對(duì)響應(yīng)的信息進(jìn)行處理
      alert(request.responseText);  // 取得響應(yīng)的內(nèi)容
    };
    // 用于替代onreadystatechange 檢測(cè)錯(cuò)誤。
    request.onerror = function(){
      // 對(duì)響應(yīng)的信息進(jìn)行處理
    };
    // 用于停止正在進(jìn)行的請(qǐng)求。
    request.onabort = function(){
      // 對(duì)響應(yīng)的信息進(jìn)行處理
      alert(request.responseText);
    };
    // 跨域發(fā)送請(qǐng)求
    request.send();
  }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論