FormData doesn't work with String on Firefox

2019-08-01 08:20发布

I made a site web that uses ajax XmlHttpRequest of level 1 and 2. With google chrome it works. But now I'm trying with other browsers and with firefox it returns this exception message:

Component returned failure code: 0x80460001 (NS_ERROR_CANNOT_CONVERT_DATA) [nsIDOMFormData.append]

now, the code that I use is the following:

try {
            var sampleFile = document.getElementById("file").files[0];
            var ext = $("#file").val().substr(
                    $("#file").val().lastIndexOf(".") + 1).toLowerCase();
            if ($("#file").val() != "" && ext != "jpg" && ext != "jpeg"
                    && ext != "bmp" && ext != "png" && ext != "gif") {
                alert("Il formato dell'immagine purtroppo non è valido!\nSono ammesse immagini JPG,JPEG,GIF,PNG e BMP.");
                return;
            }

            var nomeDaPulire = document.getElementById("nome").value;

            var cognomeDaPulire = document.getElementById("cognome").value;
            var usernameDaPulire = document.getElementById("username").value;
            var pwdDaPulire = document.getElementById("password").value;
            var pwdBisDaPulire = document.getElementById("passwordBis").value;
            var anno = document.getElementById("anno").value;
            var professione = document.getElementById("comboProfessione").value;
            var città = Pulisci(document.getElementById("comboCittà").value);
            var sessoM = document.getElementById("M").checked;
            var sesso = "";
            var nome = "";
            var cognome = "";
            var username = "";
            var pwd = "";
            var pwdBis = "";
            var email = "";
            var emailDaPulire = document.getElementById("email").value;

            if (nomeDaPulire == "" 
                    || cognomeDaPulire == "" || emailDaPulire == ""
                    || pwdDaPulire == "" || pwdBisDaPulire == ""
                    || professione == "nullo" || anno == "nullo"
                    || città == "nullo") {
                alert("Riempi tutti i campi!");
                return;
            }

            if (checkSpecial(nomeDaPulire) == false
                    || checkSpecial(cognomeDaPulire) == false
                    || checkSpecialUser(usernameDaPulire) == false
                    || checkSpecialPwd(pwdDaPulire) == false
                    || checkSpecialPwd(pwdBisDaPulire) == false
                    || checkSpecialEmail(emailDaPulire) == false) {
                alert("Per favore, non inserire caratteri speciali!");
                return;
            } else {
                nome = Pulisci(nomeDaPulire);
                cognome = Pulisci(cognomeDaPulire);
                username = Pulisci(usernameDaPulire);
                pwd = Pulisci(pwdDaPulire);
                pwdBis = Pulisci(pwdBisDaPulire);
                email = Pulisci(emailDaPulire);
            }

            if (sessoM == true) {
                sesso = "M";
            } else {
                sesso = "F";
            }

            var celiaco = document.getElementById("celiaco").checked;
            if (celiaco == true)
                cel = 1;
            else
                cel = 0;

            var lattosio = document.getElementById("lattosio").checked;
            if (lattosio == true)
                lat = 1;
            else
                lat = 0;

            var animal = document.getElementById("animal").checked;
            if (animal == true)
                an = 1;
            else
                an = 0;

            var biologico = document.getElementById("bio").checked;
            if (biologico == true)
                bio = 1;
            else
                bio = 0;

            var linea = document.getElementById("linea").checked;
            if (linea == true)
                lin = 1;
            else
                lin = 0;

            var vegan = document.getElementById("vegan").checked;
            if (vegan == true)
                veg = 1;
            else
                veg = 0;

            if (pwd.localeCompare(pwdBis) == 0) {
                    d3.select("#button").remove();
                d3.select("#buttonLine").append("img").attr("id","immLoad").attr("src",
                        "imm/progressLoad.gif");
                formdata.append("username", username);
                formdata.append("pwd", pwd);
                formdata.append("nome", nome);
                formdata.append("cognome", cognome);
                formdata.append("sesso", sesso);
                formdata.append("professione", professione);
                formdata.append("anno", anno);
                formdata.append("citt", città);
                formdata.append("lattosio", lat);
                formdata.append("glutine", cel);
                formdata.append("linea", lin);
                formdata.append("vegan", veg);
                formdata.append("biologico", bio);
                formdata.append("animal", an);
                formdata.append("sampleFile", sampleFile);
                formdata.append("email", email);
                var xhr = new XMLHttpRequest();
                xhr.open("POST", "RegistraUtente", true);

                xhr.send(formdata);

                xhr.onreadystatechange = function() {
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            var str = xhr.responseText;
                            if (str.length == 2) {
                                alert("lo username scelto è già in uso, per favore scegline un'altro!");
                            } else {
                                alert("la registrazione è avvenuta correttamente! Tra pochi istanti arriverà l'email con il link di conferma!");
                                window.location.href = "#login";
                            }
                        } else {
                            alert("error with the server");
                            d3.select("#immLoad").remove();
                            d3.select("#buttonLine").append("a").attr("class", "super button blue").attr("id",
                            "button").text("REGISTRATI").style("font-size", "16px").style(
                            "font-family", "GeezaPro, Calibri").on(
                            "click",
                            function() {
                                registrazione();
                            });
                        }
                    }
                };
            } else {
                alert("Le due password devono coincidere!");
            }
        } catch (e) {
            alert(e.message);
            return;
        }

the function Pulisci is the following:

function Pulisci(temp){
    temp=temp.ReplaceAll("è","#egrave;");
    temp=temp.ReplaceAll("à","#agrave;");
    temp=temp.ReplaceAll("ì","#igrave;");
    temp=temp.ReplaceAll("ò","#ograve;");
    temp=temp.ReplaceAll("ù","#ugrave;");
    temp=temp.ReplaceAll("é","#eacuta;");
    temp=temp.ReplaceAll("€","#euro;");
    temp=temp.ReplaceAll("°","#ordm;");
    return temp;

}

What's the problem here? why with firefox doesn't work? Help please!! thanks!

1条回答
干净又极端
2楼-- · 2019-08-01 09:04

Great, finally got sufficient information to fix your problem, you missed almost all the important things in your question.

According to specs FormData only works with Blob or DOMString. Which means it won't work with String which is an Object instead of literal string.

As Mozilla indicated, it works with Blob, File and string, otherwise, it will perform a force conversion to string. But somehow, String instance failed to be converted automatically (I think you should file it in Bugzilla,) which throws the error.

Two possible fixes:

  • Stop using new String(), when assign a literal string to a variable will automatically create a new one.
  • Force the conversion by your self, using username.toString(), and you will need to do this for all your text fields.

PS:

You probably should not use Unicode variable names, though it is fine with javascript. But looks strange. And the actual name of this question should be FormData doesn't work with String on Firefox.

查看更多
登录 后发表回答