0

I'm trying to access plain text weather data with a CORS/cross-domain get request from http://weather.noaa.gov and (reasonably obviously) I have no control over the server. The calling web page is plain HTML (ie no PHP, no ASP.NET).

The URL I'm calling for the moment is: http://weather.noaa.gov/pub/data/observations/metar/stations/LFBL.TXT and that works fine in Fiddler with the following raw response:

HTTP/1.1 200 OK
Server: Apache
ETag: "526b614-4b-5263c79234bbe"
Last-Modified: Sun, 06 Dec 2015 15:34:15 GMT
Accept-Ranges: bytes
Content-Length: 75
Content-Type: text/plain; charset=UTF-8
Cache-Control: max-age=168
Expires: Sun, 06 Dec 2015 15:44:26 GMT
Date: Sun, 06 Dec 2015 15:41:38 GMT
Connection: keep-alive

2015/12/06 15:30
LFBL 061530Z AUTO 14004KT 100V170 9999 ///TCU 12/07 Q1028

I'm using jQuery 2.1.4 and the JavaScript function that I'm using to make the call is as follows:

function getWeather(station)
{
station = station.toUpperCase();
$.ajax(
        {
        url: 'http://weather.noaa.gov/pub/data/observations/metar/stations/' + station + '.TXT', 
        type: "GET",
        //headers: { "Access-Control-Allow-Origin" : "*" } ,
        //beforeSend: function(xhr)
        //  {
        //  xhr.setRequestHeader( 'Access-Control-Allow-Origin' , '*'  )
        //  },
        dataType: "text",
        crossDomain: true,
        success: function(data)
            {
            return data;
            },
        error : function()
            {
            return "No METAR/TAF data available for the station '" + station + "'";
            }
    });
}

As shown, with the header bits commented out, the browser is rejecting the GET request with:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

So I tried a preflight calls using setRequestHeader above but the server gives this response:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

So I'm in a situation where I have to add an Access-Control-Allow-Origin header to the request but the server isn't responding appropriately (I repeat, I have no control over that) and so the call is being blocked.

Is there an error or missing configuration item in my code? Or is it impossible to retrieve text, as opposed to say XML or JSON, from a cross-domain website?

CrispinH
  • 1,899
  • 4
  • 23
  • 38
  • "Is there an error or missing configuration item in my code?" — No the configuration is missing from the site you are making the request to. They have to give your JavaScript permission to access the data they are willing to send to the owner of the browser. – Quentin Dec 06 '15 at 16:15
  • "where I have to add an Access-Control-Allow-Origin header to the request" — Wrong. Look at the error: *No 'Access-Control-Allow-Origin' header is present on the **requested resource**.* The header has to go on the response not the request. – Quentin Dec 06 '15 at 16:16
  • So what you're saying is that if *Access-Control-Allow-Origin* is not present on the requested source then what I want to achieve is not possible. Which is what I need to know. I had a look at the 'duplicate' reference you gave and none of those answers IMHO fitted the bill. They required reverse proxies, jsonp, iframes, php, third-party services that cost money. One of them did show promise, though better described here: http://www.html5rocks.com/en/tutorials/cors/ but utlimately that also ran into the problems. If it's not going to work it's not going to work and that's OK by me. – CrispinH Dec 06 '15 at 17:41

0 Answers0