Access to XMLHttpRequest at …url… from origin 'http://localhost:9000 has been blocked by CORS policy, no ‘Access-Control-Allow-Origin’ header is present on the requested resource.
I’d recommend this if you’re not super familiar with React, either that or you can look into tools like Axios to help you retrieve content from the Ghost API. There’s a pretty good tutorial here https://alligator.io/react/axios-react/
@_samayo I’m not sure what’s happening here because Ghost definitely returns the correct access-control-allow-origin header for Content API requests so that any host is allowed to connect. Here’s an example:
I’m getting the same error when developing locally and when deploying to a front end that requires some images from Ghost. I’m running Ghost at content.example.com and have the front end deployed on example.com.
I tried the curl command you shared with my Ghost URL and API key. I got the same result:
access-control-allow-origin: *
The error on Chrome reads:
Access to fetch at 'https://content.example.com/content/images/2019/10/example.jpeg' from origin 'https://example.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.
and on Safari:
Cannot load image https://content.example.com/content/images/2019/10/example.jpg due to access control checks.
Failed to load resource: Response served by service worker is opaque.
Would the best solution be to properly configure access-control-allow-origin?
How do you configure access-control-allow-origin?
I couldn’t find much on trusted domains in the Ghost docs, but some forum mentions here and here seem to suggest that is the solution. The accepted answer points to the API docs but I couldn’t find anything about CORS or trusted domains when searching there.
@eriknakamura the Content API itself has the required CORS heading to allow all origins. The trusted domains references are to the old+removed v0.1 Public API and are not relevant to the v2/v3 Content API.
In your case it’s not failing on the API but on fetching an image, the /content/images/* requests do not have any CORS headers. What is it you are doing with front-end code that requires fetching an image using JavaScript?
@eriknakamura you should be able to configure nginx (or whichever web server you have used) to add the access-control-allow-origin header to any /content/images/* requests.
Hi! I’ve started having similar problems with CORS.
Everything worked well for a while, but lately I’ve noticed the CORS error…
The weird thing is, when I CURL my ghost.io-hosted API or use javascript Ghost Content API client, it returns no access-control-allow-origin headers, but when I get the api url in the browser, there is access-control-allow-origin *
Any hints, what can be wrong here, and what I can do to fix the issue?
I am late in the party but if you have CORS issue during local development, try to check your protocol.
You should query http://localhost:{port} on localhost not https://