无论如何在Firefox中提交表单

时间:2016-01-09 09:25:03

标签: javascript ajax firefox jsonp onsubmit

var form0 = document.forms[0];

form0.onsubmit = function(e){
    alert("form submitted");
    var email = form0.elements["email"].value; 
    var url = "https://bpi.briteverify.com/emails.json?address="+email+"&apikey=my-key&callback=emailResult";

    jsonp(url, function(data) {
       if (data.status != "valid") {
        e.preventDefault();
        alert("INVALID EMAIL");
       }
    }); 
}

function jsonp(url, callback) {
    var callbackName = 'jsonp_callback_' + Math.round(100000 * Math.random());
    window[callbackName] = function(data) {
        delete window[callbackName];
        document.body.removeChild(script);
        callback(data);
    };

    var script = document.createElement('script');
    script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName;
    document.body.appendChild(script);
}

这适用于Chrome和Safari,但不适用于Firefox。虽然提交了表格"显示警报,但之后没有任何内容。

这是Firefox问题还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试此代码

form0.onsubmit = function(e){
    alert("form submitted");
    var email = form0.elements["email"].value; 
    var url = "https://bpi.briteverify.com/emails.json?address="+email+"&apikey=my-key&callback=emailResult";

    jsonp(url, function(data) {
        if (data.status != "valid") {
            alert("INVALID EMAIL");
        }
        else
        {
            form0.onsubmit = null;
            form0.submit();
        }
    }); 

    return false;
}