简体   繁体   English

jQuery Ajax beforesend, success and error with axios

[英]jQuery Ajax beforesend, success and error with axios

I have a contact form on a static web page that I'm sending via formspree.我在通过 formspree 发送的 static web 页面上有一个联系表。 I'm trying to implement ajax submit to avoid redirecting the page.我正在尝试实施 ajax 提交以避免重定向页面。

I've found this gist that does it in jQuery.我在 jQuery 中找到了这个要点

jQuery jQuery

var $contactForm = $('#contact-form');

$contactForm.submit(function(e) {
    e.preventDefault();
    var $submit = $('input:submit', $contactForm);
    var defaultSubmitText = $submit.val();

    $.ajax({
        url: '//formspree.io/your@email.com',
        method: 'POST',
        data: $(this).serialize(),
        dataType: 'json',
        beforeSend: function() {
            $submit.attr('disabled', true).val('Sending message…');
        },
        success: function(data) {
            $submit.val('Message sent!');
            setTimeout(function() {
                $submit.attr('disabled', false).val(defaultSubmitText);
            }, 5000);
        },
        error: function(err) {
            $submit.val('Oops, there was an error.');
            setTimeout(function() {
                $submit.attr('disabled', false).val(defaultSubmitText);
            }, 5000);
        }
    });
});

I would like to use only native javascript, so far this is how I've "translated it"我只想使用本机 javascript,到目前为止,这就是我“翻译它”的方式

Native JS原生JS

const form = document.querySelector('form');

form.addEventListener("submit", function(e){
    e.preventDefault();
    const $submit = form.querySelector('.contact__submit');
    const defaultSubmitText = $submit.innerHTML;

    axios({
        method: 'post',
        url: 'https://formspree.io/your@email.com',
        data: serialize(form)
        // https://plainjs.com/javascript/ajax/serialize-form-data-into-a-query-string-45/
    })
        .then(function(response) {
            console.log('SENT!!', response.data);
        })
        .catch(function(){
            console.log('ERROR!!');
        });
    //https://github.com/stefanpenner/es6-promise
});

How can i "translate" beforeSend , to let the user know that the message is sending?我如何“翻译” beforeSend ,让用户知道消息正在发送?

I know this is old but let me drop how I was able to get mine to work.我知道这是旧的,但让我放弃我是如何让我的工作的。 I had the same issue when trying to re-write my jQuery code in vanilla JS, what I did was use interceptors .当我尝试用普通 JS 重写我的 jQuery 代码时遇到了同样的问题,我所做的是使用拦截器

Using your example code使用您的示例代码

const form = document.querySelector('form');

form.addEventListener("submit", function(e){
    e.preventDefault();
    const $submit = form.querySelector('.contact__submit');
    const defaultSubmitText = $submit.innerHTML;

    // Axios BeforeSend equivalent
    axios.interceptors.request.use( config => {
       // Write whatever logic you want to run beforeSend here
        $submit.attr('disabled', true).val('Sending message…');

        return config; // very Important to add this, else Axios will cancel itself
    });

    axios({
        method: 'post',
        url: 'https://formspree.io/your@email.com',
        data: serialize(form)
        // https://plainjs.com/javascript/ajax/serialize-form-data-into-a-query-string-45/
    })
    .then(function(response) {
        console.log('SENT!!', response.data);
    })
    .catch(function(){
        console.log('ERROR!!');
    });
    //https://github.com/stefanpenner/es6-promise
});

You as well have the option to do something like BeforeReceive (ie do something before the response is returned):您也可以选择执行类似BeforeReceive的操作(即在返回响应之前执行操作):

axios.interceptors.response.use( config => {
   // Write whatever logic you want to run beforeReceieve here
    return config;
});

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

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