pschlan / cron-job.org

cron-job.org Open Source project
GNU General Public License v2.0
1.45k stars 251 forks source link

Sending a request from front-end javascript results in CORS excpetion #133

Closed wc-davide closed 2 years ago

wc-davide commented 2 years ago

Hi, I'm trying to implement the API in my front-end javascript website but I'm getting a cors exception, running the same request through Insomnia works just fine, so I'm kinda lost...

On the profile settings page I can only set IPs, not hosts... is my use case not meant to work? The key is retrieved from a db only for logged in admin users, so it's not being exposed to the public...

My request:

const req = await fetch('https://api.cron-job.org/jobs', {
    headers: {
        'Authorization': `Bearer ${API_KEY}`,
        'Content-Type': 'application/json',
    },
})

Error in console:

Access to fetch at 'https://api.cron-job.org/jobs' from origin 'http://localhost:4000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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.

Any help would be appreciated, thanks!

pschlan commented 2 years ago

Hi,

thanks for the report. I made some changes, can you try again? Thanks.

wc-davide commented 2 years ago

Hi,

thanks for the report. I made some changes, can you try again? Thanks.

Now it works perfectly 😄

Thanks!