跨来源请求被阻止-如何在没有访问服务器的情况下绕过它

Cross-Origin Request Blocked - How to get around it without having access to the server

本文关键字:服务器 访问 情况下 请求      更新时间:2023-09-26

我正在创建一个自定义主页,它要求我从各个网站获取数据(例如天气)。

我一直在使用以下代码:

var req = new XMLHttpRequest()
req.open('GET', 'http://weather.com/en-GB/weather/today/l/...')
req.send()
req.addEventListener('load', function() {
    console.log(this.responseText)
})

在发送之前,我也尝试过req.withCredentials = true,但没有什么不同。

我发现了关于这方面的各种问题,但其中大多数用户都可以访问服务器。我唯一能找到的没有它的是:

Firefox CORS请求给予';跨来源请求被阻止';尽管标题

没有一个答案说明如何绕过这个问题,所以我的问题是-我如何绕过这个错误

尝试使用jsonp

$.ajax({
   url: 'http://weather.com/en-GB/weather/today/l/SS7:4:UK',
   dataType: 'JSONP',
   jsonpCallback: 'callback',
   type: 'GET',
   success: function (data) {
   console.log(data);
}
});

https://learn.jquery.com/ajax/working-with-jsonp/