[英]How to GET simple data from these sites properly without CORS error
I have a problem getting data from this two sites: 我从这两个站点获取数据时遇到问题:
http://app.lotto.pl/wyniki/?type=dl http://app.lotto.pl/wyniki/?type=dl
and 和
Eurolottery . 欧洲彩票 。
I know that the problem is associated with CORS and I managed to get data using Chrome CORS extension and that code: 我知道问题与CORS有关,我设法使用Chrome CORS扩展程序和以下代码获取数据:
var HttpClient = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open( "GET", aUrl, true );
anHttpRequest.send( null );
}
}
var client = new HttpClient();
client.get('http://app.lotto.pl/wyniki/?type=dl', function(response) {
var currentDraw = response;
x = currentDraw.split("\n").slice(1, 7).sort().join(" ");
console.log(x);
}
);
And I tried to use CORS tutorial code with that: 而且我尝试使用CORS教程代码:
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
return xhr;
}
// Helper method to parse the title tag from the response.
function getTitle(text) {
return text.match('<title>(.*)?</title>');
}
// Make the actual CORS request.
function makeCorsRequest() {
// This is a sample server that supports CORS.
var url = 'http://api.bentasker.co.uk/lottopredict/?action=LatestResults&key=751fbf6ddfb7c3857d898c21bfdc2b22&game=3&draws=Any';
var xhr = createCORSRequest('GET', url);
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = getTitle(text);
console.log(text.split("\n"));
};
xhr.onerror = function() {
alert('Woops, there was an error making the request.');
};
xhr.send();
}
makeCorsRequest();
But in both situations it doesn't work properly. 但是在两种情况下它都无法正常工作。 What is the easiest way to get data from sites posted above using javascript?
使用javascript从上面发布的网站获取数据的最简单方法是什么?
如果您不想设置自己的服务器,则可以使用https://cors-anywhere.herokuapp.com/之类的东西-在简单的情况下它可以很好地工作。
If you don't have control to the Host, you will need to use a proxy (ie: CORS-Anywhere ) to remove the CORS policy. 如果您无法控制主机,则需要使用代理(即: CORS-Anywhere )来删除CORS策略。
var HttpClient = function() { this.get = function(aUrl, aCallback) { var anHttpRequest = new XMLHttpRequest(); anHttpRequest.onreadystatechange = function() { if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200) aCallback(anHttpRequest.responseText); } anHttpRequest.open( "GET", aUrl, true ); anHttpRequest.send( null ); } } var client = new HttpClient(); client.get('https://cors-anywhere.herokuapp.com/http://app.lotto.pl/wyniki/?type=dl', function(response) { var currentDraw = response; x = currentDraw.split("\\n").slice(1, 7).sort().join(" "); console.log(x); } );
More ways to handle the CORS Policy locally are available here . 这里提供了更多在本地处理CORS政策的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.