前面的重要说明:如果另一端的服务器没有启用它,则您在自己的服务器上无能为力客户端允许跨源ajax请求的代码。
在回答你的问题之前,先给你介绍一下背景:
同源安全策略 http://en.wikipedia.org/wiki/Same-origin_policy
简而言之,同源安全策略可确保来自一个源的脚本不能从其他源获取内容。现在为了向您解释起源的概念,让我引用部分内容同源安全策略的维基百科文章 http://en.wikipedia.org/wiki/Same-origin_policy:
下表概述了针对 URL 检查的典型结果“http://www.example.com/dir/page.html http://www.example.com/dir/page.html".
Compared URL Outcome Reason
------------------------------------------------------- ------- ----------------------
http://www.example.com/dir/page2.html Success Same protocol and host
http://www.example.com/dir2/other.html Success Same protocol and host
http://username:[email protected] /cdn-cgi/l/email-protection/dir2/other.html Success Same protocol and host
http://www.example.com:81/dir/other.html Failure Same protocol and host but different port
https://www.example.com/dir/other.html Failure Different protocol
http://en.example.com/dir/other.html Failure Different host
http://example.com/dir/other.html Failure Different host (exact match required)
http://v2.www.example.com/dir/other.html Failure Different host (exact match required)
http://www.example.com:80/dir/other.html Depends Port explicit. Depends on implementation in browser.
与其他浏览器不同,Internet Explorer 不将端口包含在源计算中,而是使用安全区域来代替。
因此,例如,您的 JavaScript 无法从其来源服务器以外的 Web 服务器下载任何内容(也称为向其发出 HTTP 请求)。这正是您无法向其他域发出 XmlHttpRequests(又名 AJAX)的原因。
CORS 是一种方法另一端的服务器(不是浏览器中的客户端代码)可以放宽同源政策 http://en.wikipedia.org/wiki/Same-origin_policy#Relaxing_the_same-origin_policy.
过于简单化的描述跨源资源共享 (CORS) http://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
CORS 标准通过添加新的 HTTP 标头来工作,这些标头允许服务器向允许的源域提供资源。浏览器支持这些标头并尊重它们建立的限制。
Example: 假设您的网站是http://my-cool-site.com
并且,您在域中有第三方 APIhttp://third-party-site.com
,您可以通过 AJAX 访问。
让我们假设来自您服务器的页面my-cool-site.com
向third-party-site.com
。通常,用户浏览器将拒绝对除您自己的域/子域之外的任何其他站点的 AJAX 调用同源安全策略 http://en.wikipedia.org/wiki/Same_origin_policy。但如果浏览器和第三方服务器支持 CORS,则会发生以下情况:
-
浏览器将发送和Origin
HTTP 标头到third-party-site.com
Origin: http://my-cool-site.com
-
如果第三方服务器接受来自您的域的请求,它将用一个响应Access-Control-Allow-Origin
HTTP 标头:
Access-Control-Allow-Origin: http://my-cool-site.com
-
要允许所有域,第三方服务器可以发送此标头:
Access-Control-Allow-Origin: *
如果您的网站不被允许,浏览器将抛出错误。
如果客户有相当现代的支持 CORS 的浏览器 http://caniuse.com/cors,以及您的第三方服务器支持跨域资源共享 http://client.cors-api.appspot.com/client同样,CORS 对您也很有用。
在一些过时的浏览器(例如 IE8)中,您必须使用 Microsoft 特定的XDomainRequest
对象而不是XMLHttpRequest
进行可以正确使用 CORS 的调用;现在这已经过时了,所有现代浏览器(包括来自 Microsoft 的浏览器)都处理 CORSXMLHttpRequest
反而。但如果您需要支持过时的浏览器,这一页 http://jquery-howto.blogspot.com/2013/09/jquery-cross-domain-ajax-request.html#cors描述它:
要发出 CORS 请求,您只需使用XMLHttpRequest
在 Firefox 3.5+、Safari 4+ 和 Chrome 中XDomainRequest
IE8+ 中的对象。使用时XMLHttpRequest
对象,如果浏览器发现您正在尝试发出跨域请求,它将无缝触发 CORS 行为。
这是一个 JavaScript 函数,可以帮助您创建跨浏览器 CORS 对象。
function createCORSRequest(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
// XHR has 'withCredentials' property only if it supports CORS
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined"){ // if IE use XDR
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
同样,这仅适用于过时的浏览器。
上述原因是您无法通过脚本使用 Amazon 的 Web 服务的原因。亚马逊服务器只允许将 JavaScript 文件下载到选定域提供的页面。
来回答您的numbered问题:
- 如果文件是同源的,浏览器就会下载该文件。
- 如果不是同源,则 CORS 请求成功后将下载该文件。
- 否则,下载脚本会失败。
- 如果下载成功,JavaScript 文件的内容将被加载到浏览器的内存中,并被解释和执行。
请参阅 CORS 的说明来了解。