FORMDATA不与字符串在Firefox工作 [英] FormData doesn't work with String on Firefox

查看:423
本文介绍了FORMDATA不与字符串在Firefox工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的,使用Ajax XmlHtt $ P $ 1级pquest和2。谷歌浏览器它的工作原理网站网页。但现在我想与其他浏览器,并与Firefox返回此异常消息:

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]

现在,我使用code是如下:

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;
        }

功能Pulisci如下:

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;

}

有什么问题吗?为什么用Firefox无法正常工作? 请帮助!! 谢谢!

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

推荐答案

太好了,终于得到了足够的信息来解决您的问题,你在你的问题错过了几乎所有重要的事情。

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

根据规格 FORMDATA 只适用于的Blob DOMString 。这意味着它不会与字符串这是一个对象而不是字面字符串工作

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.

由于Mozilla的表示,它的工作原理与的Blob 文件字符串,否则会执行力转化为字符串。但不知何故,字符串的实例未能自动转换(我想你应该提交它的 Bugzilla的,),它引发错误。

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.

有两种可能的修正:

  • 停止使用新的String(),赋予文字字符串时,变量将自动创建一个新的。
  • 将你的自我强制转换,使用 username.toString(),你将需要做到这一点对所有的文本字段。
  • 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:

您可能不应该使用统一code变量名,但它是精细与JavaScript。但是,看起来怪怪的。而这个问题的实际名称应该是 FORMDATA不与字符串在Firefox工作的。

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.

这篇关于FORMDATA不与字符串在Firefox工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆