Created
June 30, 2015 02:26
-
-
Save joeylin/7755c50f8676197bb876 to your computer and use it in GitHub Desktop.
CORS
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
function createCORSRequest(method, url) { | |
var xhr = new XMLHttpRequest(); | |
if ("withCredentials" in xhr) { | |
// 此时即支持CORS的情况 | |
// 检查XMLHttpRequest对象是否有“withCredentials”属性 | |
// “withCredentials”仅存在于XMLHTTPRequest2对象里 | |
xhr.open(method, url, true); | |
} else if (typeof!= "undefined") { | |
// 否则检查是否支持XDomainRequest,IE8和IE9支持 | |
// XDomainRequest仅存在于IE中,是IE用于支持CORS请求的方式 | |
xhr = new XDomainRequest(); | |
xhr.open(method, url); | |
} else { | |
// 否则,浏览器不支持CORS | |
xhr = null; | |
} | |
return xhr; | |
} | |
var xhr = createCORSRequest('GET', url); | |
if (!xhr) { | |
throw new Error('CORS not supported'); | |
} |
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
res.setHeader('Access-Control-Allow-Origin', '*'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment