Ich habe eine einfache ASP.NET Web API-Anwendung mit individuellen Konten für die Authentifizierung.jQuery + ASP.NET Web API + CORS Fehler
I aktiviert CORS drin: installiertes Paket und das hinzugefügt:
var cors = new EnableCorsAttribute(
origins: "*",
headers: "*",
methods: "*");
config.EnableCors(cors);
in die Datei WebApiConfig.
Jetzt möchte ich Anfragen von einer einfachen Webseite mit jQuery machen. Ich habe diesen Code jetzt:
var loginData = {
grant_type: "password",
username: "foo",
password: "Something_1562"
};
$.ajax({
type: 'POST',
url: "https://localhost:44351/Token",
data: loginData
}).done(function (data) {
alert(data.userName);
alert(data.access_token);
}).fail(function (data) {
alert('Request Status: ' + req.status + ' Status Text: ' + req.statusText + ' ' + req.responseText);
});
Und ich bekomme diese:
XMLHttpRequest cannot load https://localhost:44351/Token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
EDIT:
Einige zusätzliche Informationen: - Ich habe https/ssl im Web api ermöglichen - Das API läuft auf ISS Express
, die den Trick tat! Vielen Dank. –
Btw, ich habe jetzt ein anderes Problem: meine GET-Anfragen werden in OPTIONS geändert, was zu einem 405-Code führt. Ich habe einige Posts gelesen, aber nichts funktioniert ... Irgendwelche Tipps? –
@ CátiaAzevedo Vielleicht auch diese Links sind auch hilfreich: http://enable-cors.org/server_iis7.html und http://StackOverflow.com/Questions/28488535/CORS-settings-for-IIS7-5 – jyrkim