Javascript-CORS没有响应

Javascript - CORS no response

本文关键字:响应 Javascript-CORS      更新时间:2024-06-07

我有网站:http://www.bluegreenblack.com/p/weather-in-ireland.html

想要从以下位置添加Metar读数:https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=检索&format=xml&hoursBeforeNow=3&mostResent=true&stationString=EIDW

我想在CORS中使用Javascript。

这是代码(从其他网站复制):

<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=检索&format=csv&hoursBeforeNow=3&mostResent=true&stationString=EIDW。请求的资源上不存在"Access Control Allow Origin"标头。原点'http://www.bluegreenblack.com因此不允许访问。

我使用python urlib2获取有效数据,这有任何用处。。。

很可能服务器不支持CORS请求。您是否尝试过从web服务器调用此服务,并创建自己的web服务来为您的页面提供服务?