ReactJs ์ฑ์์ get ์์ฒญ์ ์คํํ ๋ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
"XMLHttpRequest๋ http://localhost :3002/api/users๋ฅผ ๋ก๋ํ ์ ์์ต๋๋ค. ์คํ ์ ์์ฒญ์ ๋ํ ์๋ต์ด ์ก์ธ์ค ์ ์ด ๊ฒ์ฌ๋ฅผ ํต๊ณผํ์ง ๋ชปํ์ต๋๋ค: ์์ฒญํ ๋ฆฌ์์ค์ 'Access-Control-Allow-Origin' ํค๋๊ฐ ์์ต๋๋ค. Origin ' http: //localhost :3005' ๋ฐ๋ผ์ ์ก์ธ์ค๊ฐ ํ์ฉ๋์ง ์์ต๋๋ค. ์๋ต์ HTTP ์ํ ์ฝ๋ 404๊ฐ ์์์ต๋๋ค."
๋ด ์ฝ๋.
const ROOT_URL = ' http://localhost :3002';
const AUTH_TOKEN = 'eyJ0eXAiOiJKV1QiLCJhb.....';
axios.defaults.baseURL = ROOT_URL;
axios.defaults.headers.common['์ธ์ฆ'] = AUTH_TOKEN;
axios.defaults.headers.post['์ฝํ
์ธ ์ ํ'] = '์์ฉ ํ๋ก๊ทธ๋จ/json;charset=utf-8';
axios.get('/api/users').then(ํจ์(์๋ต) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});
์ด๊ฒ์ axios ๋ฌธ์ ๊ฐ ์๋๋๋ค. CORS ์์ฒญ์ ์๋ฝํ๋๋ก ์๋ฒ๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค. http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource ์ ๋ํ ์ฒซ ๋ฒ์งธ ๋ต๋ณ์๋ณด์ญ์์ค
์๋
,
๋๋ ์ด๋ฏธ ๊ทธ๊ฒ์ ์ฌ์ฉํ๋ค. ์ถ๊ฐ๋ ์๋ฒ์์ ๊ณ ์
"๊ฒฝ๋ก": {
"์ฝ๋ฅด": ์ฌ์ค
}
Hapi ๊ตฌ์ฑ ์ฐ๊ฒฐ ์
๊ฐ์ฌํฉ๋๋ค.
์๋ ํ์ธ์ @9tor ๊ฐ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ์ต๋๊น?
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด๊ฒ์ axios ๋ฌธ์ ๊ฐ ์๋๋๋ค. CORS ์์ฒญ์ ์๋ฝํ๋๋ก ์๋ฒ๋ฅผ ์ค์ ํด์ผ ํฉ๋๋ค. http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource ์ ๋ํ ์ฒซ ๋ฒ์งธ ๋ต๋ณ์๋ณด์ญ์์ค