2016-04-13 34 views
0

私はウェブサイトを持っている:私はCORSにJavascriptを使用したいhttps://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&stationString=EIDWJavascriptを - CORS無応答

http://www.bluegreenblack.com/p/weather-in-ireland.html

からMETARの測定値を追加したいです。ここで

コード(他の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 =検索&形式= csv &時間BeforeNow = 3 & mostRecent = true & stationString = EIDW。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。 Origin 'http://www.bluegreenblack.com'はアクセスできません。

私のpythonを使用して、有効なデータを取得するが、これは...あらゆる使用のサーバがCORS要求をサポートしていないこと

+0

サーバーがそれをサポートしていないかのように見えます。ページのドメインとは異なるドメインに対してXMLHttpRequestが作成されました。これは、サーバーが応答ヘッダーに "Access-Control-Allow-Origin"ヘッダーを返すことを要求しますが、1つは返されませんでした。サーバーはCORS要求をサポートし、リソースとともに適切な "Access-Control-Allow-Origin"ヘッダーを返す必要があります。応答ヘッダーのCORSの詳細については、IE10のXHRのCORSを参照してください。このような場合は。私の選択肢は何ですか? – Vic152

+0

一つのオプションはあなたのウェブサーバーに** aviationweather **へのリクエストを行い、テキストまたはjsonまたは何かの結果を返すページを作成し、javacript経由でこのページを呼び出すことができます – Omar

答えて

0

最も可能性があり、それをurllib2の。あなたのWebサーバーからこのサービスを呼び出そうとしましたが、自分のページにサービスを提供する独自のWebサービスを作成しようとしましたか?

+0

私はアクセスが非常に限られています。私のウェブはブロガーのラッパーです。 – Vic152

+0

あなたはJavaスクリプトで立ち往生しています:(天気予報のための別のドメイン@ vic152が見つかりません –

+0

私はこれをウェブで見つけました:https://www.aviationweather.gov/help/webservice?page=metarjson私はいません大きなウェブプログラマーなので、私にはダブルダッチです... – Vic152

関連する問題