You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I'm learning the combo Vercel / NextJS / SWR by trying to make a simple fetch component, but I haven't find how to fetch REST api from outside sources(CORS), I try to modify this example:
import useSWR from 'swr'
const fetcher = (...args) => fetch(...args,).then(res => res.json())
function Profile () {
const { data, error } = useSWR('/api/user/123', fetcher)
if (error) return <div>failed to load</div>
if (!data) return <div>loading...</div>
// render data
return <div>hello {data.name}!</div>
}
to
import useSWR from 'swr'
function Profile() {
const URL = 'https://43.129.77.73:61525/access/'
const fetcher = (...args) => fetch(...args,).then(res => res.json())
const { data, error } = useSWR(URL, {
mode: 'cors',
headers: {
'Access-Control-Allow-Origin': '*'
}
}, fetcher)
if (error) return <div>failed to load</div>
if (!data) return <div>loading...</div>
// render data
return <div>hello {data.code}!</div>
}
it simply doesn't work with no error message and returns <div>failed to load</div>.
I am sure I didn't use it in the right way, so does SWR support CORS REST request? If it is possible, what is the right way? Thanks so much for help! :)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Hi! I'm learning the combo Vercel / NextJS / SWR by trying to make a simple fetch component, but I haven't find how to fetch REST api from outside sources(CORS), I try to modify this example:
to
it simply doesn't work with no error message and returns
<div>failed to load</div>
.I am sure I didn't use it in the right way, so does SWR support CORS REST request? If it is possible, what is the right way? Thanks so much for help! :)
Beta Was this translation helpful? Give feedback.
All reactions