Access-Control-Allow-Origin-header mehrere Werte ODER Preflight-jquery-ajax-Web-Api 2
Ich habe eine asp.net web-Formulare Anwendung auf localhost:6414 Dann die jquery-ajax - ruft ein Web-Api-service auf localhost:11974
Ich die Fehlermeldung
The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:6414' is therefore not allowed access.
Also ich Kamm durch google und stackoverflow und finden Sie Tonnen von widersprüchliche Informationen.
1. Fix with web.config
2. Fix with jquery ajax call
3. Fix with CORS in web.api
Habe ich versucht das Cors aktiviert und web.config und alle Arten von Techniken.
Derzeit Frage ich mich, ob das problem tatsächlich die web forms-jquery-code, der als Absender, aber eigentlich ist die Antwort-Header würde bedeuten, es ist von der web-api-service...
Ich schaue auf meine Antwort-Header und ich sehe 2 dieser
Access-Control-Allow-Origin:* ( repeated TWICE)
Ich versuchen, kommentieren Sie diese Zeile in web.config in Web-Api-Projekt
<add name="Access-Control-Allow-Origin" value="*" />
DANN bekomme ich diese Fehler 401:
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:6414' is therefore not allowed access. The response had HTTP status code 401.
Habe ich diesen code im web.config
<authentication mode="Windows" />
<authorization>
<allow verbs="OPTIONS" users="*" />
<deny users="?" />
</authorization>
Nun, wenn ich kommentieren die Access-Control.... in web.config und DANN Kommentar aus der CORS code in WebApiConfig.cs
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
Wenn dieser code auskommentiert, DANN bekomme ich die preflight-Fehler
XMLHttpRequest cannot load http://localhost:11974/PostCheckBox. Response for preflight has invalid HTTP status code 405
Dieser scheint völlig verrückt.
Jquery-code
function sendCheckboxes(id, out, yes, no) {
//$(function () {
//var storm = { id: 1902, StormOut: 1, StormYes: 1, StormNo: 1 };
var storm = { id: id, StormOut: out, StormYes: yes, StormNo: no };
//jQuery.support.cors = true;
$.ajax({
type: "POST",
data: JSON.stringify(storm),
url: "http://localhost:11974/PostCheckBox",
contentType: "application/json",
//crossDomain: true,
success: function (data) {
console.log(data)
},
error: function (x, y, z) {
console.log(x + '\n' + y + '\n' + z);
}
});
//});
}
Web-api-Methode
[Route("PostCheckBox")]
public HttpResponseMessage PostCheckBox([FromBody] StormData storm)
{.... }
Wenn ich diese Methode aufrufen, bis aus derselben Domäne (jetzt gleichen port auf localhost) funktioniert Es einwandfrei
http://localhost:11974/checkbox.html
Was gibt...
Du musst angemeldet sein, um einen Kommentar abzugeben.
Sie kämpfen Ihre web.config
Kommentieren Sie alle die Scheiße, die Sie Hinzugefügt, um die web -.config!!!
Benutze ich nicht, und ich bin SICHER, dass das Problem 1
Auch ein anderer ist, dass Sie brauchen, um Kommentar aus
dann VERWENDEN diese Zeilen
Können Sie immer noch in Sicherheit , Authentifizierung und Autorisierung und Cors Einzelheiten auf Anfrage nicht global, sondern auf Klasse oder Methode, aber wenn Sie sind mit eine harte Zeit mit nur immer die Sache zu arbeiten, dann sollte diese ja auch funktionieren.
<add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="*" /> <add name="Access-Control-Allow-Methods" value="*" />
Aktivieren cors in startup.cs