2016-12-01 13 views
0

私はこのqquestionが何度も尋ねられましたが、私の問題を解決していないことを知っています。私はサーバからトークンを取り出すためにaxiosを使用しています(​​)。私はlocalhostに取り組んでいます。私はあまりにもヘッダーを供給しているが、まだ私はNo 'Access-Control-Allow-Origin' header is presentのエラーを取得します。このエラーは私のjsコード(axios)から生成されていますか、それともサーバーの問題ですか?要求されたリソースに 'Access-Control-Allow-Origin'ヘッダーが存在しません

const config = { 
    method: 'get', 
    url: `${API_URL}/token`, 
    headers: { 
     'Access-Control-Allow-Origin': '*', 
     'Content-Type': 'application/json', 
     }, 
    }; 

export function getToken() { 
    return function (dispatch) { 
    console.log('action triggered atleast'); 
    axios.request(config) 
    .then(response => { 
     console.log('response', response); 
     localStorage.setItem('token', response.data.token); 
     dispatch({ type: GET_TOKEN }); 
    }) 
    .catch(error => { 
     errorHandler(dispatch, error.response, TOKEN_ERROR); 
    }); 
    }; 
} 

私のコードに何か間違いや不足がありますか?

UDPATE

add_header 'Access-Control-Allow-Origin' "*"; 
     add_header 'Access-Control-Allow-Credentials' 'true'; 
     add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; 
     add_header 'Access-Control-Allow-Headers' 'X-CustomHeader,Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With'; 
     # required to be able to read Authorization header in frontend 
     add_header 'Access-Control-Expose-Headers' 'Authorization' ; 

if ($request_method = 'OPTIONS') { 
     # Tell client that this pre-flight info is valid for 20 days 
     add_header 'Access-Control-Max-Age' 1728000; 
     add_header 'Content-Type' 'text/plain charset=UTF-8'; 
     add_header 'Content-Length' 0; 
     return 204; 
} 

上記の私のnginxの構成です。 http://enable-cors.org/server.html

をあなたが多分あなたがしたいサーバへのアクセスを持っていない場合:

答えて

2

サーバは、要求

はCORSを有効にする方法をここで見てみましょうではないバックにCORSヘッダを送信する必要がありますcors proxy

+0

フロントエンドに問題はありません。問題はサーバーにあります。私はnginxサーバーを持っていて、私はこの 'Access-Control-Allow-Origin'のようなことをしてCORSを有効にする必要があります: '*'しかし、独自の方法ではなくjsの方法ですか? – Serenity

+0

問題がサーバーにあることを修正しました(nginx) – Endless

+0

あなたとあなたが共有したリンクから理解していただきありがとうございます。ほとんどのそのような質問では、私はヘッダーの使用を見てどこかにサーバーの問題が発生したので、私はちょうど問題がどこにあるのか混乱してしまいました。あなたのサポートとお手伝いをもう一度感謝します。 – Serenity

関連する問題