SmartableAI / developer-program

Smartable AI Developer Program
https://developer.smartable.ai
4 stars 0 forks source link

CORS block #2

Closed jparish3 closed 4 years ago

jparish3 commented 4 years ago

I am trying to access the COVID-19 from a web app in development using local host. I have tried to use the request configurations for javascript but keep getting blocked by CORS (see below). Do you have a fetch or axios config that would work ? the API looks great and I would like to resolve my issue. Thanks

...has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

SmartableAI commented 4 years ago

Just a quick note letting you know that we are looking into it now. We purposefully configured the api to allow all ALL origins to call the API. We are checking what's wrong with that configuration.

SmartableAI commented 4 years ago

Hey @jparish3 ! It was a config issue. The problem is fixed! Please try again and let us know whether it works or not.

SmartableAI commented 4 years ago

Here is my test code running in a localhost website. It's able to retrieve the data correctly with our config fix.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$.ajax({
    type: "GET",
    url: "https://api.smartable.ai/coronavirus/stats/US",

    // Request headers
    beforeSend: function(xhrObj) {
        xhrObj.setRequestHeader("Cache-Control", "no-cache");
        xhrObj.setRequestHeader("Subscription-Key", "<replace this with your own key>");
        },
    })
.done(function (data) {
    alert(data.updatedDateTime);
})
.fail(function () {
    alert("error");
});
</script>
jparish3 commented 4 years ago

thanks for the quick fix. I am able to get through using axios, but have not figured out to pass credentials using fetch:

fetch('https://api.smartable.ai/coronavirus/news/CA', { 'Cache-Control': 'no-cache', 'Subscription-Key': '' }) .then(response => response.json()) .then(data => { console.log(JSON.stringify(data)) }) returns: "statusCode": 401, "message": "Access denied due to missing subscription key. Make sure to include subscription key when making requests to an API." }

let me know if you have any suggestions for fetch , but axios will work fine

SmartableAI commented 4 years ago

You need to get a free subscription key by signing up or signing into our developer portal (https://developer.smartable.ai), go to your profile, click 'generate a new subscription' if you haven't done so, choose 'Free APIs', give it a name and click 'Subscribe'. Then you will get a key. Copy and paste the key to

'Subscription-Key': '<put the key here>'

And you should be able to call the API

SmartableAI commented 4 years ago

get-the-key

Here is a screenshot of the last step to retrieve the key.