如何通过 HTML5 允许不同域中的受限资源进入网络浏览器
跨源资源共享 (CORS) 是一种机制,允许网络浏览器中另一个域中的受限资源
假设您单击 html5 演示部分中的 HTML5 视频播放器。它会询问相机权限。如果用户允许权限,则只打开相机,否则它不会为网页应用程序打开相机
此处 Chrome、Firefox、Opera 和 Safari 都使用 XMLHttprequest2 对象,而 Internet Explorer 使用类似的 XDomainRequest 对象。
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
}
else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
}
else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
var xhr = createCORSRequest('GET', url);
if (!xhr) {
throw new Error('CORS not supported');
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP