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

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

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

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

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

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

  // 跨瀏覽器創(chuàng)建并返回CORS對象
  // param method : 請求的方式, get or post
  // param url : 跨域請求的url
  // return xhr : 返回的跨域資源對象
  function createCORSRequest(method, url){
    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr){
      xhr.open(method, url, true);  // CORS都是通過異步的請求
    } 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 檢測成功,表示接受數(shù)據(jù)完畢
    request.onload = function(){
      // 對響應(yīng)的信息進行處理
      alert(request.responseText);  // 取得響應(yīng)的內(nèi)容
    };
    // 用于替代onreadystatechange 檢測錯誤。
    request.onerror = function(){
      // 對響應(yīng)的信息進行處理
    };
    // 用于停止正在進行的請求。
    request.onabort = function(){
      // 對響應(yīng)的信息進行處理
      alert(request.responseText);
    };
    // 跨域發(fā)送請求
    request.send();
  }

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

相關(guān)文章

最新評論