Hey everyone! I’m working on a web project using NextJS for the frontend and Gin (Go) for the backend. I’ve run into a CORS error and I’m wondering if it could be coming from the frontend side. I’m using axios to talk to my Gin backend API.
Here’s a snippet of my code:
// DataFetcher.tsx
useEffect(() => {
async function grabData() {
setIsLoading(true);
try {
const result = await fetchItems(currentPageNum, itemsPerPage, apiEndpoint);
updateItemList(result.data.items);
updateTotalCount(result.data.item_count);
} catch (err) {
setErrorMessage('Failed to get data');
} finally {
setIsLoading(false);
}
}
grabData();
}, [currentPageNum, itemsPerPage, apiEndpoint]);
// apiCalls.ts
export async function fetchItems(page, limit, endpoint) {
try {
const divider = endpoint.includes('?') ? '&' : '?';
const fullUrl = `${endpoint}${divider}page=${page}&limit=${limit}&query`;
const response = await api.get(fullUrl);
return response.data;
} catch (err) {
console.error('Oops:', err);
}
}
Has anyone run into CORS issues that were actually caused by frontend code? Any tips would be super helpful!