Enonic version: 7.9.2
OS: Ubuntu 20.04
I’m creating a project using NextXP, where I’m developing a search feature.
On my page search, which is a Layout component, I want to perform a query using the URL param and then render the results in a list.
I’m using the useEffect
hook to do this.
First, I tried using the fetchFromApi
method:
useEffect(()=>{
const urlSearchParams = new URLSearchParams(window.location.search);
const searchTerm = urlSearchParams.get('s') || '';
const body = {
query: `query ($path: String!) {
guillotine {
query(query: $path) {
_path
displayName
type,
dataAsJson
}
}
}`,
variables: {
path: `fulltext('displayName, data.*, page.*, x.*', '${searchTerm}', 'AND')`
},
}
const response = await fetchFromApi('http://localhost:8080/site/hmdb/master', body)
}, []);
When I execute this on the Content Studio preview, on the localhost:8080
, it works well.
But, when I try this on the localhost:3000
, I got an CORS error from the Enonic side:
Access to fetch at 'http://localhost:8080/site/hmdb/master' from origin 'http://localhost:3000' 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.
I also tried using an Enonic service instead of the fetchFromApi
method, but I got the same CORS error (even allowing the CORS headers in the service file).
How can I solve this? I’m doing something wrong on my approach?