Warum scheint CORS nicht zur Arbeit mit POST?
-
23-09-2019 - |
Frage
Mozilla eigene Spezifikation sagt einfach GET
oder POST
sollte ohne Preflighting aber bisher nativen CORS ist seine jeder POST
Versuch, den ich gemacht habe, hat in einem OPTIONS
Header Ausgehen geführt. Wenn ich es von POST
ändern sendet den Code zu erhalten sofort eine richtige GET
Anfrage der Cross-Site-Teil fein arbeitet.
Hier ist eine abgespeckte Probe von dem, was ich in Firefox so mache:
var destinationUrl = 'http://imaginarydevelopment.com/postURL';
var invocation = new XMLHttpRequest();
if (invocation) {
invocation.open('POST', destinationUrl, true);
//tried with and without this line
//invocation.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
invocation.onreadystatechange = (function Handler() {
if (invocation.readyState == 4)
alert('Request made');
});
invocation.send(/* tried with and without data*/);
}
Hier ist, was ich schon in Chrom und IE hatte arbeiten:
var destinationUrl = 'http://imaginarydevelopment.com/postURL';
var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError,
dataType: 'text', contentType: 'application/x-www-form-urlencoded'
};
destination.data = { 'rows': rowList, 'token': token };
$jq.ajax(destination);
Lösung 2
gut, ich weiß nicht, was alle Content tatsächlich Arbeit aber text/plain
tut auf allen 3 Browsern:
var destination = { url: destinationUrl, type: 'POST', success: AjaxSuccess, error: AjaxError,
contentType: 'text/plain'
};
var postData={ 'anArray': theArray, 'token': token };
destination.data=JSON.stringify(postData);
$jq.ajax(destination);
Doch bisher habe ich nicht herausgefunden, was die Anforderung hindert von etwas zu tun, um den Erfolg Methode auch neben laufen, wenn ein 505-Code zurückgegeben wird. Hinzufügen einer Antwort-Header von Access-Control-Allow-Origin: *
der Browser nicht wollen gelöst, um die Rückgabedaten zu lesen.
Andere Tipps
Ich habe das gleiche Problem
https://developer.mozilla.org/En/HTTP_Access_Control
sagt, dass der enctype sein muss text / plain oder Sie brauchen Fx4 + verwenden Alle Zugriffs Header korrekt eingestellt werden