表单提交后如何重定向(Javascript)?

时间:2019-03-04 06:52:44

标签: javascript

这是我从中获得的资源(请参阅“直接通过网络浏览器部分”部分)https://www.espocrm.com/documentation/administration/web-to-lead/

问题是,提交后如何重定向此表单?

react-icons
<script type="text/javascript">
    var webToLeadFormElement = document.getElementById('web-to-lead-form');
    var webToLeadFormIsSubmitted = false;

    webToLeadFormElement.addEventListener('submit', function (event) {
        event.preventDefault();
        if (webToLeadFormIsSubmitted) return;
        webToLeadFormIsSubmitted = true;
        webToLeadFormElement.submit.setAttribute('disabled', 'disabled');

        var payloadData = {
            firstName: webToLeadFormElement.firstName.value,
            lastName: webToLeadFormElement.lastName.value,
            emailAddress: webToLeadFormElement.emailAddress.value
        };

        // Needed url can be found on Lead Capture detail view.
        var url = 'https://URL_OF_YOUR_CRM/api/v1/LeadCapture/API_KEY'; 

        var xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', 'application/json');
        xhr.setRequestHeader('Accept', 'application/json');
        xhr.onreadystatechange = function() {
            if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
                var containerElement = document.getElementById('web-to-lead-form-container');
                containerElement.innerHTML = 'Sent';
            }
        }
        xhr.onerror = function() {
            webToLeadFormElement.submit.removeAttribute('disabled');
            webToLeadFormIsSubmitted = false;
        }
        xhr.send(JSON.stringify(payloadData));
    });
</script>

1 个答案:

答案 0 :(得分:0)

您可以使用:

xhr.onreadystatechange = function() {
    if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
       var containerElement = document.getElementById('web-to-lead-form-container');
       containerElement.innerHTML = 'Sent';
       setTimeout(function(){
            // SHow MSG and after 2 sec redirect 
            window.location.href=url;
       } , 2000)
      }
    }

      //  window.location.href=url

成功或失败后进行重定向。