简体   繁体   English

FormData不适用于Firefox上的String

[英]FormData doesn't work with String on Firefox

I made a site web that uses ajax XmlHttpRequest of level 1 and 2. With google chrome it works. 我创建了一个使用ajax XmlHttpRequest 1级和2级的网站。使用谷歌浏览器它可以工作。 But now I'm trying with other browsers and with firefox it returns this exception message: 但是现在我正在尝试使用其他浏览器并使用firefox返回此异常消息:

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: Pulisci的功能如下:

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? 为什么用firefox不起作用? Help please!! 请帮助!! thanks! 谢谢!

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 . 根据规范, FormData仅适用于BlobDOMString Which means it won't work with String which is an Object instead of literal string . 这意味着它不会与工作String这是一个Object ,而不是文字string

As Mozilla indicated , it works with Blob , File and string , otherwise, it will perform a force conversion to string . 正如Mozilla 所说 ,它适用于BlobFilestring ,否则,它将执行强制转换为string But somehow, String instance failed to be converted automatically (I think you should file it in Bugzilla ,) which throws the error. 但不知何故, String实例无法自动转换(我认为你应该在Bugzilla中提交它),这会引发错误。

Two possible fixes: 两个可能的修复:

  • Stop using new String() , when assign a literal string to a variable will automatically create a new one. 停止使用new String() ,当为变量分配文字字符串时,将自动创建一个新字符串。
  • Force the conversion by your self, using username.toString() , and you will need to do this for all your text fields. 使用username.toString()强制您自己进行转换,您需要对所有文本字段执行此操作。

PS: PS:

You probably should not use Unicode variable names, though it is fine with javascript. 你可能不应该使用Unicode变量名,尽管javascript很好。 But looks strange. 但看起来很奇怪。 And the actual name of this question should be FormData doesn't work with String on Firefox . 而这个问题的实际名称应该是FormData不能与Firefox上的String一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM