Created
March 16, 2012 13:07
-
-
Save mywaiting/2050004 to your computer and use it in GitHub Desktop.
跨域的XMLHTTPRequest封装,配合IFrame的兼容实现!采摘自InstaPaper的实现,仔细功能还需要细化,但基本可以使用!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@Description: 发送跨域XMLHTTPRequest到Server,Server必须回应HTTP头部 Access-Control-Allow-Origin | |
@Support: IE8+, Firefox 3.5+, Safari 4+, Opera 9+, Chrome | |
@Refer: | |
http://www.denisdeng.com/?p=1024 | |
http://blog.csdn.net/doraeimo/article/details/7329779 | |
*/ | |
function createXDPostRequest(method, url, payload, success, error) { | |
try { | |
if (window.XDomainRequest) { | |
var xdr = new XDomainRequest(); | |
if (!xdr) { | |
throw (0); | |
} | |
xdr.onerror = error; | |
xdr.ontimeout = function() {}; | |
xdr.onload = function() { | |
success(xdr.responseText); | |
} | |
xdr.open(method, url); | |
xdr.send(payload); | |
} else if (window.XMLHttpRequest) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open(method, url, true); | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
xhr.onreadystatechange = function() { | |
try { | |
if (xhr.readyState != 4) return; | |
if (xhr.status != 200) throw (0); | |
success(xhr.responseText); | |
} catch (e) { | |
error(); | |
} | |
} | |
xhr.send(payload); | |
} else { | |
error(); | |
} | |
} catch (e) { | |
error(); | |
} | |
} | |
function createIframePostRequest(method, url, payload, options) { | |
// Default | |
options.iframeId = options.iframeId || "XDIframeForPostRequest"; | |
options.iframeOnload = options.iframeOnload || function(){}; | |
// Create Iframe | |
var ifr = document.createElement("iframe"); | |
ifr.setAttribute("id", options.iframeId); | |
ifr.setAttribute("name", options.iframeId); | |
ifr.setAttribute("allowtransparency", "true"); | |
ifr.setAttribute("style", "border: 0; width: 0; height: 0; display: noen;"); | |
ifr.setAttribute("onload", options.iframeOnload); | |
document.body.appendChild(ifr); | |
// Get iframe's Window's Document | |
var ifwin = window.frames[options.iframeId]; | |
var ifdoc = ifwin.contentDocument || ifwin.contentWindow.document; | |
// Create Form | |
var strings = '<html>'; | |
strings += '<body>'; | |
strings += '<form action="'+ url +'" method="'+ method +'" id="iframeformforpoat">'; | |
strings += '<input type="hidden" name="payload" id="payload" value=""'; | |
strings += '</form>'; | |
strings += '<script>'; | |
strings += 'var e=encodeURIComponent,w=window,d=document,f=d.getElementById("iframeformforpoat");'; | |
strings += 'd.getElementById("payload").value=decodeURIComponent(' + payload + ')'; | |
strings += 'd.getElementById("iframeformforpoat").submit();'; | |
ifdoc.write(strings); | |
} | |
/** | |
@Description: 整合iframe和XMLHttpRequest的方法,iframe提交后成功与否这里不做处理 | |
@Support: IE6+, Firefox, Safari, Opera, Chrome | |
@Notice: 在中国大陆的某些IE6内核的浏览器可能会有安全策略阻止插iframe,导致整个请求失败,这里不做处理 | |
*/ | |
function XDRequest(method, url, payload) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment