Last active
December 18, 2019 23:27
-
-
Save LouisdeBruijn/574f86df2b3d606149a06228fabfcb57 to your computer and use it in GitHub Desktop.
send the AJAX request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function modular_ajax(url, type, formData) { | |
| // Most simple modular AJAX building block | |
| $.ajax({ | |
| url: url, | |
| type: type, | |
| data: formData, | |
| processData: false, | |
| contentType: false, | |
| beforeSend: function() { | |
| // show the preloader (progress bar) | |
| $('#form-response').html("<div class='progress'><div class='indeterminate'></div></div>"); | |
| }, | |
| complete: function () { | |
| // hide the preloader (progress bar) | |
| $('#form-response').html(""); | |
| }, | |
| success: function ( data ){ | |
| if ( !$.trim( data.feedback )) { // response from Flask is empty | |
| toast_error_msg = "An empty response was returned."; | |
| toast_category = "danger"; | |
| } | |
| else { // response from Flask contains elements | |
| toast_error_msg = data.feedback; | |
| toast_category = data.category; | |
| } | |
| }, | |
| error: function(xhr) {console.log("error. see details below."); | |
| console.log(xhr.status + ": " + xhr.responseText); | |
| toast_error_msg = "An error occured"; | |
| toast_category = "danger"; | |
| }, | |
| }).done(function() { | |
| M.toast({html: toast_error_msg, classes: 'bg-' +toast_category+ ' text-white'}); | |
| }); | |
| }; | |
| var csrf_token = "{{ csrf_token() }}"; | |
| $.ajaxSetup({ | |
| beforeSend: function(xhr, settings) { | |
| if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) { | |
| xhr.setRequestHeader("X-CSRFToken", csrf_token); | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment