AJAX 由另一个域的 javascript 文件发出的请求
AJAX requests made by another domain's javascript file
在同一台计算机上有两个 Visual Studio 的 运行,具有以下 URL:
http://localhost:47503(网络 api,#1)
http://localhost:12345(web端调用客户端api,#2)
客户端 (#2) 硬编码站点 #1 的 <script src="http://localhost:47503/file.js"></script>
。 file.js 内部是一个函数:
function GetData() {
$.ajax({
url: 'http://localhost:47503/api/autos',
type: 'GET',
dataType: 'json',
data: { "a": '_1', "b": 'TEST', "c": "val c" },
success: function (result) {
alert("success");
}
});
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at
http://localhost:47503/api/autos?a=_1&b=TEST&c=value+c.
(Reason: CORS header 'Access-Control-Allow-Origin' missing).
是否认为由于脚本是从客户端 #1 加载的,它可以从客户端 #2 调用从而以某种方式绕过同源策略?来自 Client #1 的脚本回调到 Client #1。 JSONP 和 CORS 是已知的问题解决方案,但想知道以上是否可行。
Thought that since script was loaded from Client #1, that it could be invoked from Client #2 hence bypassing the same origin policy in a way?
不,这是不可能的。来源始终是文档的来源,而不是单个脚本标签。这就是您如何从 CDN 加载像 jQuery 这样的库,但向您自己的服务器发出 AJAX 请求。
附带说明一下,即使来源是由进行调用的脚本确定的(从来没有),您的代码仍然无法正常工作,因为它实际上是 jQuery 进行 AJAX呼唤。
Cross-Origin Request Blocked
您的服务器 #1 可以通过在其 HTTP 响应中添加 header "Access-Control-Allow-Origin" 来明确告诉 Web 浏览器它实际上接受来自另一个域的请求。
在同一台计算机上有两个 Visual Studio 的 运行,具有以下 URL:
http://localhost:47503(网络 api,#1)
http://localhost:12345(web端调用客户端api,#2)
客户端 (#2) 硬编码站点 #1 的 <script src="http://localhost:47503/file.js"></script>
。 file.js 内部是一个函数:
function GetData() {
$.ajax({
url: 'http://localhost:47503/api/autos',
type: 'GET',
dataType: 'json',
data: { "a": '_1', "b": 'TEST', "c": "val c" },
success: function (result) {
alert("success");
}
});
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:47503/api/autos?a=_1&b=TEST&c=value+c. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
是否认为由于脚本是从客户端 #1 加载的,它可以从客户端 #2 调用从而以某种方式绕过同源策略?来自 Client #1 的脚本回调到 Client #1。 JSONP 和 CORS 是已知的问题解决方案,但想知道以上是否可行。
Thought that since script was loaded from Client #1, that it could be invoked from Client #2 hence bypassing the same origin policy in a way?
不,这是不可能的。来源始终是文档的来源,而不是单个脚本标签。这就是您如何从 CDN 加载像 jQuery 这样的库,但向您自己的服务器发出 AJAX 请求。
附带说明一下,即使来源是由进行调用的脚本确定的(从来没有),您的代码仍然无法正常工作,因为它实际上是 jQuery 进行 AJAX呼唤。
Cross-Origin Request Blocked
您的服务器 #1 可以通过在其 HTTP 响应中添加 header "Access-Control-Allow-Origin" 来明确告诉 Web 浏览器它实际上接受来自另一个域的请求。