我有网站:http://www.bluegreenblack.com/p/weather-in-ireland.html
的Metar读数我想在CORS中使用Javascript。
以下是代码(来自其他website的copypasta):
<script language="Javascript">
// Create the XHR object.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
xhr.setRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
} 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>')[1];
}
// Make the actual CORS request.
function makeCorsRequest() {
// All HTML5 Rocks properties support CORS.
var url = 'https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=csv&hoursBeforeNow=3&mostRecent=true&stationString=EIDW';
var xhr = createCORSRequest('GET', url);
xhr.setRequestHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8')
//xhr.setRequestHeader("Access-Control-Allow-Origin", '*');
if (!xhr) {
alert('CORS not supported');
return;
}
// Response handlers.
xhr.onload = function() {
var text = xhr.responseText;
var title = getTitle(text);
alert(text);
//alert('Response from CORS request to ' + url + ': ' + title);
};
xhr.onerror = function() {
//alert('Woops, there was an error making the request.');
alert(xhr.statusText);
};
xhr.send();
}
makeCorsRequest()
</script>
我得到回应:
**General**
Request URL:https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=csv&hoursBeforeNow=3&mostRecent=true&stationString=EIDW
Request Method:GET
Status Code:200 OK
Remote Address:140.90.101.207:443
**Response Headers**
Connection:Keep-Alive
Content-Type:application/x-csv
Date:Wed, 13 Apr 2016 21:28:12 GMT
Keep-Alive:timeout=300, max=98
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Via:1.1 aviationweather.ncep.noaa.gov
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-XSS-Protection:1; mode=block
**Request Headers**
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8, text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
DNT:1
Host:www.aviationweather.gov
Origin:http://www.bluegreenblack.com
Referer:http://www.bluegreenblack.com/p/weather-in-ireland.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
**Query String Parameters**
view URL encoded
dataSource:metars
requestType:retrieve
format:csv
hoursBeforeNow:3
mostRecent:true
stationString:EIDW
但没有回应......
我在日志中遇到此错误:
XMLHttpRequest无法加载https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSourc ... pe = retrieve&amp; format = csv&amp; hoursBeforeNow = 3&amp; mostRecent = true&amp; stationString = EIDW。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://www.bluegreenblack.com”访问。
我使用python urllib2获取有效数据,这是有用的......