2017-05-21 3 views
0

https://example.com fire ajax Vorbitte (beforeSend) zu https://api.example.com (nginx)Anforderungsheaderfeld Die Autorisierung ist in der Preflight-Antwort von Access-Control-Erlaube-Header nicht zulässig. (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) {} 
}); 

Chromkonsole return Fehlermeldung

XMLHttpRequest https://api.example.com/auth kann nicht geladen werden. Anforderungsheaderfeld Die Autorisierung ist in der Preflight-Antwort von Access-Control-Allow-Header nicht erlaubt.

Antwort

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

Ist das eine Lösung? Ich frage mich nur, weil es keine Erklärung dafür gibt, was das ist oder wo dieser Code gehört –

Verwandte Themen