Javascript-CORS没有回应 [英] Javascript - CORS no response
问题描述
我有网站: http://www.bluegreenblack.com/p/weather-in-ireland.html
Wanted to add Metar readings from: https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=EIDW
我想在CORS中使用Javascript.
I want to use Javascript with CORS.
Here is the code(copypasta from other website):
<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& format = csv&hoursBeforeNow = 3& mostRecent = true& stationString = EIDW.所请求的资源上没有"Access-Control-Allow-Origin"标头.因此,不允许访问来源" http://www.bluegreenblack.com .
XMLHttpRequest cannot load https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSourc…pe=retrieve&format=csv&hoursBeforeNow=3&mostRecent=true&stationString=EIDW. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.bluegreenblack.com' is therefore not allowed access.
我使用python urllib2获取有效数据,这有什么用...
I get valid data using python urllib2 it this is of any use...
推荐答案
该服务器很可能不支持CORS请求.您是否尝试过从Web服务器调用此服务并创建自己的Web服务来为您的页面服务?
most likely that server does not support CORS requests. Have you tried to call this service from your web server and create your own web service to serve for your page?
这篇关于Javascript-CORS没有回应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!