Replies: 14 comments
-
Me too see https://www.mprnews.org/weather I now get CORS errors I've been using this client side only for about a year
|
Beta Was this translation helpful? Give feedback.
-
Of course it's a workaround, but you can always use https://cors-anywhere.herokuapp.com/. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue. In case it's not clear: The API spec says you should provide a User-Agent header, specifically a custom, unique one to avoid being involved with "security incidents" and get security notifications. But if you provide this, it is no longer a "simple request" for CORS, and requires a pre-flight request. The CORS preflight response from weather.gov does not list In other words, to actually use the API you cannot specify a User-Agent header (a custom one, in code) until they fix their preflight responses. |
Beta Was this translation helpful? Give feedback.
-
@nearwood will you detail the environment that you're making the request? CORS implies you're using it within a browser, but a browser will set that for you (currently Safari and Chrome break spec and won't even let you set the user agent). |
Beta Was this translation helpful? Give feedback.
-
@vplus10 and @ghankerson sorry this reply took too long--are you both able to make requests? |
Beta Was this translation helpful? Give feedback.
-
I can if I omit the User-Agent, this is in Chrome and Firefox. IIRC, Chrome won't let you and will fail, Firefox fails on the CORS preflight. I thought it was preferable to set the user-agent to something to avoid being associated with others abusing the API, etc.? Or is that just for non-browser applications? |
Beta Was this translation helpful? Give feedback.
-
Yes, the suggestion to make it unique is for server-side applications (or proxies). Using the default UA in a client-side application is fine. (I'll update the documentation later this week to make that more clear.) We are also working on transitioning to API keys (would say by end of year, but nothing official yet). |
Beta Was this translation helpful? Give feedback.
-
Here is a basic example that works in Chrome and Firefox: https://jsfiddle.net/4sfkL9v7/ |
Beta Was this translation helpful? Give feedback.
-
Ah, OK -- then it (in my case at least) was just an issue of not understanding the docs. Thanks! |
Beta Was this translation helpful? Give feedback.
-
I'm unable to resolve this issue. I would really appreciate some help. Is it possible to send the headers with JavaScript? I'm trying to use something like this:
but still getting that error. I have also tried sending it via php first, but the app is in JavaScript. |
Beta Was this translation helpful? Give feedback.
-
@BenGoliwas without the whole xmlhttp block I can't tell exactly, but that works if used correctly. I've updated my example to demonstrate: https://jsfiddle.net/4sfkL9v7/. Note the setRequestHeader has to come after the "open" but before the "send." |
Beta Was this translation helpful? Give feedback.
-
I really appreciate you answering this. That is still not working for me. Access to fetch at 'https://api.weather.gov/alerts/active' from origin 'https://the-sprinter.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. Is it possible for you to show a complete JavaScript file? Must the header be sent with php or some other server-side language? This is my current state. If you're able, can you tell me how to get this to work? It is most appreciated... array( 'method'=>"GET", 'header'=>"Accept: application/geo+json;version=1\r\n" . "User-agent: $UserAgent\r\n" ) ); $context = stream_context_create($opts); ?> <style> body { font-family: Arial, Helvetica, sans-serif; font-size: 12px; } </style> <script>//START CLOCK
} //SET GLOBAL VARIABLE FOR THE SECOND CALL //GET LOCATION //GET INITIAL CALL
//GET FORECAST
window.onload=function(){ |
Beta Was this translation helpful? Give feedback.
-
And then it's now working for me. Should it do that (sometimes work / sometimes not)? |
Beta Was this translation helpful? Give feedback.
-
Not working on Safari. |
Beta Was this translation helpful? Give feedback.
-
I am writing code in HTML5, javascript, and jquery to retrieve data via the NWS api. when using a jquery ajax call I am getting "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.weather.gov/stations/KBBG/observations/latest. (Reason: header ‘user-agent’ is not allowed according to header ‘Access-Control-Allow-Headers’ from CORS preflight response)." when it set the User-Agent as required by the api specifications. I am using the setRequestHeader to do so.
Any insight would be appreciated
Thank you.
function GetLatest(station) {
var nwsUrl = 'http://api.weather.gov/stations/'+station+'/observations/latest';
return $.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader('User-Agent', navigator.userAgent + ' vannet.noip.us([email protected])');
xhr.setRequestHeader('Accept', 'application/ld+json;version=1');
},
type: "GET",
dataType: 'json',
url: nwsUrl,
success: function(data) {
console.log(data);
current = data;
}
});
}
Beta Was this translation helpful? Give feedback.
All reactions