从外部源提取数据并获得No'访问控制允许来源'标头存在于请求的资源上

pulling data from external source and getting a No 'Access-Control-Allow-Origin' header is present on the requested resource

本文关键字:存在 于请求 资源 请求 数据 提取 No 从外部 访问控制      更新时间:2023-09-26

我试图从外部源提取JS文件,并得到以下错误:

请求的资源上不存在"Access Control Allow Origin"标头。因此,不允许访问源"null"。

我试图提取的文件是一个JS文件,里面有数据

var xhr = new XMLHttpRequest();
xhr.open("get", "https://s3-ap-southeast-2.amazonaws.com/[userid]/data.js", true);
xhr.onload = function(){  //instead of onreadystatechange
    //do something
    alert("here");
};
xhr.send(null);

我也试过

var promise = $.ajax({
    url: "https://s3-ap-southeast-2.amazonaws.com/[userid]/data.js",
    method: 'GET'
});
promise.done(function (result) {
    alert("done");
}).fail(function(){
    alert("Data cannot be loaded at this time. Please ensure there are no ad blockers enabled.");
});

我已经用JSON文件尝试过了,这些都有效,但它不会加载到JS文件中。有人能发光吗?

您需要在获取数据的响应中添加CORS头。没有其他办法。

相关文章: