[英]How do I get HTML page from a website with AJAX request without getting CORS error?
I'm trying to do basic webscraping with javascript code using XMLHttpRequest.我正在尝试使用 XMLHttpRequest 对 javascript 代码进行基本的网页抓取。 The code is triggered when I click on a button in an html page that I open with my Firefox browser.
当我单击使用 Firefox 浏览器打开的 html 页面中的按钮时,将触发代码。
The code I'm using is below.我正在使用的代码如下。 It tries to get the html page from ATP tour website and to print it as first step.
它尝试从 ATP 旅游网站获取 html 页面并将其作为第一步打印。 Then I will try to parse it to get the information I want (like a rank for example).
然后我会尝试解析它以获得我想要的信息(例如排名)。
let htmlRequest = new XMLHttpRequest();
htmlRequest.open("GET", "https://www.atptour.com/en/players/matteo-berrettini/bk40/overview");
htmlRequest.onreadystatechange = function () {
if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
console.log(this.responseText);
}
};
htmlRequest.send();
I discovered that running this code on localhost does not work because I'm not on the same domain as the site I'm sending my request (it is the classical CORS issue).我发现在 localhost 上运行此代码不起作用,因为我与发送请求的站点不在同一个域中(这是经典的 CORS 问题)。
I now understand I need to create a web server but my understanding of the problem stops here.我现在明白我需要创建一个 web 服务器,但我对问题的理解到此为止。
So my question is: what do I have to implement to make my js script working?所以我的问题是:我必须实现什么才能使我的 js 脚本正常工作?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.