JavaScript 跨域請求庫:XDomain

jopen 10年前發布 | 12K 次閱讀 XDomain JavaScript開發

XDomain 是 JavaScript CORS 跨域請求的一個替代產品,無需任何服務器端的配置。只需要在同域下放置一個 proxy.html 文件即可。該庫利用 XHook 來獲取所有 XHR,可以無縫的和其他庫協同工作。

特性

  • Simple

  • Library Agnostic

    • With jQuery  $.ajax (and subsequently $.get, $.post)

    • With Angular $http service

  • Cross domain XHR just magically works

  • Easy XHR access to file servers:

  • Includes XHook and its features

  • proxy.html files (slaves) may:

    • White-list domains

    • White-list paths using regular expressions (e.g. only allow API calls: /^\/api/)

  • Highly performant

  • Seamless integration with FormData

瀏覽器支持情況:
JavaScript 跨域請求庫:XDomain
示例代碼:

//do some vanilla XHR
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://xyz.example.com/secret/file.txt');
xhr.onreadystatechange = function(e) {
  if(xhr.readyState === 4)
    alert(xhr.responseText);
};
xhr.send();

//or if we are using jQuery...
$.get('http://xyz.example.com/secret/file.txt').done(function(data) {
  console.log("got result: ", data);
});

項目主頁:http://www.baiduhome.net/lib/view/home/1405219618957

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!