Javascript - CORS 无响应

Posted

技术标签:

【中文标题】Javascript - CORS 无响应【英文标题】:Javascript - CORS no response 【发布时间】:2016-08-05 06:00:15 【问题描述】:

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

想要添加来自:https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=EIDW 的 Metar 读数

我想将 javascript 与 CORS 一起使用。

这是代码(来自其他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&format=csv&hoursBeforeNow=3&mostRecent=true&stationString=EIDW。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'http://www.bluegreenblack.com'。

我使用 python urllib2 获取有效数据,这有什么用...

【问题讨论】:

看起来好像服务器可能不支持:向与您页面的域不同的域发出了 XMLHttpRequest。这要求服务器在其响应标头中返回一个“Access-Control-Allow-Origin”标头,但没有返回一个。服务器必须支持 CORS 请求并随资源返回适当的“Access-Control-Allow-Origin”标头。有关响应标头中 CORS 的更多信息,请参阅 IE10 中 XHR 的 CORS。如果是这种情况。我有哪些选择? 一个选项可以是在您的网络服务器中创建一个页面,该页面将向 aviationweather 发出请求,然后将结果作为文本或 json 或其他内容返回,并通过以下方式调用此页面改为 javascript 【参考方案1】:

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

【讨论】:

我的访问权限非常有限。我的网络是博客的包装器。 所以你坚持使用 java 脚本 :( 你为什么不为天气报告找到另一个域 @vic152 我在网上找到了这个:aviationweather.gov/help/webservice?page=metarjson我不是一个大的网络程序员,所以它对我来说是双重荷兰... @vic152 找到另一个域。

以上是关于Javascript - CORS 无响应的主要内容,如果未能解决你的问题,请参考以下文章

AWS 无服务器自定义 jwt 授权方 lambda 设置 cors 响应

无法在 CORS 请求中得到响应

响应于预检要求不经过访问控制检查(JavaScript的)

django-cors-headers 和 nginx 配置:预检响应缺少 CORS 标头

Ajax 不接受 CORS(预检)响应

Cors XmlHttpRequest 和响应