2017-05-21 10 views
0

https://example.com火災AJAXプレリクエスト(beforeSend)要求ヘッダーフィールドプリフライト応答でAccess-Control-Allow-Headersによる許可が許可されていません。 (nginxの)<a href="https://api.example.com" rel="nofollow noreferrer">https://api.example.com</a>(nginxの)へ

$.ajax({ 
    method: "POST", 
    url: 'https://api.example.com', 
    xhrFields: {withCredentials: true}, 
    data: {...}, 
    success: function(msg) {...}, 
    beforeSend: function(request){ 
     var token = 'xxxxxx'; 
     request.setRequestHeader('Authorization', 'Bearer ' + token); 
    }, 
    complete: function(msg) {}, 
    error: function(xhr, ajaxOptions, thrownError) {} 
}); 

クロムコンソールリターンエラーメッセージ

のXMLHttpRequestはhttps://api.example.com/authをロードできません。要求ヘッダーフィールド許可は、プリフライト応答でAccess-Control-Allow-Headersによって許可されません。

答えて

0
location/{ 
    if ($request_method = OPTIONS) { 
     add_header Access-Control-Allow-Origin "https://example.com"; 
     add_header Access-Control-Allow-Methods "GET, OPTIONS"; 
     add_header Access-Control-Allow-Headers "Authorization"; 
     add_header Access-Control-Allow-Credentials "true"; 
     add_header Content-Length 0; 
     add_header Content-Type text/plain; 
     return 200; 
    } 
} 
+3

これは解決策ですか?これが何であるか、またはこのコードがどこに属しているかについての説明がないので、ちょうど不思議です –

関連する問題