简体   繁体   English

无法在Epihpany浏览器上执行Ajax请求

[英]Unable to perform ajax request on epihpany browser

I am currently working on a website and I need to send a form without performing a redirection. 我目前在网站上工作,我需要发送表单而不执行重定向。

I have done the following to do so : 我已经做到以下几点:

my_form.addEventListener('submit',function (event){
    event.preventDefault();
    var request = new XMLHttpRequest();
    var request_process = function (){
        if (request.readyState == 4) {
            if(request.status == 200){
                window.location.replace('nice_URL');
            }else if(request.status == 401){
                login_display_error('Authentification error : '+request.responseText);
            }else{
                alert('Server response, '+request.status+' :'+request.responseText);
            }
        }
    };
    var requestURL = new String();
    requestURL = "requestURL.com";

    request.addEventListener("readystatechange", request_process, false);
    request.withCredentials = true;
    request.open("POST",requestURL , true);
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    var parameters = new URLSearchParams(new FormData(my_form)).toString();
    request.send(parameters);
});

It works fine on Google Chrome, Chromium, Firefox Quantum but does not work with Epiphany browser and some older browser. 它在Google Chrome,Chromium,Firefox Quantum上运行良好,但不适用于Epiphany浏览器和某些较旧的浏览器。

I want to stay in vanilla JS. 我想留在香草JS中。

The problem seems to be that new URLSearchParams(new FormData(my_form)).toString(); 问题似乎是新的URLSearchParams(new FormData(my_form))。toString(); returns an empty string. 返回一个空字符串。

Does anyone had the same issue ? 有人遇到过同样的问题吗?

See the MDN documentation which shows the URLSearchParams is very shiny and new and thus does not have wide browser support. 请参阅MDN文档该文档显示URLSearchParams非常闪亮和新颖,因此不具有广泛的浏览器支持。

It links to a Google blog entry and the library which in turn links to a polyfill . 它链接到Google博客条目和库,库又链接到polyfill

Of course, that's a third-party library that isn't distributed with the browser, so probably doesn't count as vanilla JS , so you will have to reimplement it from scratch. 当然,这是未随浏览器一起分发的第三方库,因此可能不算作原始JS ,因此您必须从头开始重新实现它。

So I ended up creating my own function. 因此,我最终创建了自己的函数。

Thanks Quentin for your answer. 感谢Quentin的回答。

function serialize (form, expected){
    var encoded = new String();
    var elements = form.elements;
    var i;
    for(i=0; i<elements.length; i++){
        let e = elements[i];
        if (expected.includes(e.name)){
            if (e.multiple){
                let options = e.options;
                let j;
                for(j=0; j<options.length;j++){
                    let o = options[j];
                    if(o.selected && !o.disabled){
                        encoded = encoded.concat('&',e.name,'=',o.value);
                    }
                }
            }else{
                encoded = encoded.concat('&',e.name,'=',e.value);
            }
        }
    }
    return encoded.slice(1);
}

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

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