¿Por qué mi solicitud POST-dominios siendo verificación previa a la solicitud de Opciones?

StackOverflow https://stackoverflow.com/questions/3489631

  •  28-09-2019
  •  | 
  •  

Pregunta

De acuerdo con el Mozilla Developer Center control de acceso HTTP artículo , a través del sitio de la POST las solicitudes pueden ser "simple" - es decir, no requieren verificación previa -. Si la solicitud de Content-Type es application/x-www-form-urlencoded

Hasta ahora no recibo este comportamiento en Firefox, y no voy a entender del todo por qué esto es así. Aquí está mi código de configuración:

function makeXDomainRequest(url, method, data) {
    var req =
        typeof XDomainRequest !== "undefined" ?
        new XDomainRequest() : new XMLHttpRequest();

    req.open(method || "GET", url, true);

    if (typeof req.onload !== "undefined") {
        req.onload = onResponseLoad;
        req.onerror = onRequestError;
    } else {
        req.onreadystatechange = onRequestStateChange;
    }

    if (data && typeof req.setRequestHeader === "function") {
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    } else {
        // no way to set Content-Type req header in IE's XDomainRequest:
        // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx
    }

    req.send(data || null);
}

function onResponseLoad() {
    alert("Response!\n" + this.responseText);
}

function onRequestError(args) {
    alert("Error!");
}

function onRequestStateChange() {
    if (this.readyState === 4) {
        if (this.status === 200) {
            onResponseLoad.apply(this);
        } else {
            onRequestError.apply(this);
        }
    }
}

Y aquí está el servidor que estoy ping:

// thanks to http://saltybeagle.com/cors/ for having this demo endpoint:
var URL = "http://ucommbieber.unl.edu/CORS/cors.php";

Ahora bien, si hago una simple solicitud de POST - con los datos enviados como application/x-www-form-urlencoded en el código anterior - la solicitud de verificación previa está en Firefox con solicitar una OPCIONES. No es verificación previa en Chrome. Abrir Fiddler antes de ejecutar este para ver por sí mismo:

makeXDomainRequest(URL, "POST", "name=foobar");
// alerts "Response! Hello CORS [...] You sent a POST request. Your name is foobar"

A continuación se solicitan las OPCIONES de verificación previa en Fiddler (aviso de la cabecera Access-Control-Request-Method: POST a pesar de que he especificado un cabeceras no personalizadas supuestamente segura Content-Type y):

OPTIONS http://ucommbieber.unl.edu/CORS/cors.php HTTP/1.1
Host: ucommbieber.unl.edu
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST

¿Qué está pasando? Es esto un error en Firefox, o estoy haciendo algo mal? Gracias!

¿Fue útil?

Solución

De hecho, resultó ser un error Firefox. Se terminó siendo fijada para FF4b6: https://bugzilla.mozilla.org/show_bug.cgi ? id = 588920

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top