[英]What headers are necessary to complete an XMLHttpRequest?
I am trying to access images from a Google Photos album and populate a webpage with those images.我正在尝试访问 Google 相册相册中的图像并使用这些图像填充网页。 This goal has been completed before , but implemented with Axios (which I'm not very familiar with).
这个目标之前已经完成,但是是用Axios实现的(我不是很熟悉)。 My issue is that my origin (currently a local port) is not allowed by Access-Control-Allow-Origin.
我的问题是 Access-Control-Allow-Origin 不允许我的来源(当前是本地端口)。 From the tutorials and examples I've followed, including this one from MDN , I thought my implementation allows the port I'm running on, but I still run into the same issue.
从我遵循的教程和示例中,包括来自 MDN 的这个,我认为我的实现允许我正在运行的端口,但我仍然遇到了同样的问题。
// This is not the actual album url; for privacy I have changed it
const ALBUM_URL = "https://my-google-photos-album-url";
$(document).ready(function()
{
GetAlbum();
});
function GetAlbum()
{
const xhr = new XHRHttpRequest();
xhr.open("GET", ALBUM_URL);
// Set the origin to the port we will be using
xhr.setRequestHeader("Access-Control-Allow-Origin", "http://localhost:8000");
xhr.onreadystatechange = function(data)
{
console.log(data);
}
xhr.send();
}
Expected Behavior: Print a string of HTML and JS as was shown in the linked example .预期行为:打印一串 HTML 和 JS,如链接示例中所示。
Actual Behavior: Gives an error indicating that "http://localhost:8000" is not allowed.实际行为:给出错误指示“http://localhost:8000”不被允许。 Exact details are given below.
下面给出了确切的细节。
Error log:错误日志:
Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.
Access-Control-Allow-Origin 不允许 Origin http://localhost:8000。
XMLHttpRequest cannot load https://my-google-photos-album-url due to access control checks.
由于访问控制检查,XMLHttpRequest 无法加载 https://my-google-photos-album-url。
Failed to load resource: Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.
无法加载资源:Access-Control-Allow-Origin 不允许 Origin http://localhost:8000。
What am I missing here?我在这里缺少什么? I have also tried setting "Access-Control-Allow-Origin" to "*", but without any luck.
我也尝试将“Access-Control-Allow-Origin”设置为“*”,但没有任何运气。 Any and all help is useful, thanks!
任何和所有帮助都是有用的,谢谢!
The CORS error that you get is returned by the server that you are trying to access - the server behind the https://my-google-photos-album-url
.您收到的 CORS 错误由您尝试访问的服务器返回 -
https://my-google-photos-album-url
后面的服务器。
If you have access to this server, you should set there the CORS policy that will allow the client ( http://localhost:8000
in your case) to access its resources.如果您有权访问此服务器,则应在那里设置 CORS 策略,以允许客户端(在您的情况下为
http://localhost:8000
)访问其资源。 Otherwise, I'm not sure you can access it.否则,我不确定您是否可以访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.