XDomainJavaScript 跨域請求庫
XDomain 是 JavaScript CORS 跨域請求的一個替代產(chǎn)品,無需任何服務器端的配置。只需要在同域下放置一個 proxy.html 文件即可。該庫利用 XHook 來獲取所有 XHR,可以無縫的和其他庫協(xié)同工作。
Features
Simple
Library Agnostic
Cross domain XHR just magically works
No need to modify the server code
No need to use IE's silly XDomainRequest Object
Easy XHR access to file servers:
proxy.htmlfiles (slaves) may:White-list domains
White-list paths using regular expressions (e.g. only allow API calls:
/^\/api/)Highly performant
Seamless integration with FormData
示例代碼:
//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);
});評論
圖片
表情
