0

My code for an ajax form is not giving the form data to express and instead giving a 302 redirect to the same page

// process the form
        $('#my_form').submit(function(event) {
            console.log('Debug 1');
            event.preventDefault(); // stop the form from submitting the normal way and refreshing the page
            console.log('Debug 2');
            // toggle loader
            $('#toggle').css("display", "block");
            // Populate hidden form on submit
            var content = document.querySelector('input[name=content]');
            //- content.value = JSON.stringify(quill.root.innerHTML);
            content.value = JSON.stringify(quill.getContents());
            console.log('Debug 3');
            var post_url = $(this).attr("action"); //get form action url
            var request_method = $(this).attr("method"); //get form GET/POST method
            var form_data = new FormData(this); //Encode form elements for submission
            console.log('Debug 4');
            // process the form
            $.ajaxSetup({
                headers: {'X-CSRF-Token': "#{_csrf}"}
            });
            console.log('Debug 5');
            $.ajax({
                url : post_url,
                type: request_method,
                data : form_data,
                processData:false,
                contentType: false
            }).done(function(data) {
                console.log('Debug 6');
            });
        });

Chrome's console then shows the following

jobs:15 Debug 1
jobs:17 Debug 2
jobs:24 Debug 3
jobs:28 Debug 4
jobs:33 Debug 5
jobs:41 Debug 6
jquery-3.2.1.js:8706 XHR finished loading: GET "http://localhost:8080/company/jobs".
send @ jquery-3.2.1.js:8706
ajax @ jquery-3.2.1.js:8136
(anonymous) @ jobs:34
dispatch @ jquery-3.2.1.js:5095
elemData.handle @ jquery-3.2.1.js:4766

While my node console has the following

POST /company/new 302 613.501 ms - 56
GET /company/jobs 200 776.382 ms - -

Why is my from being rejected by express with a 302

dev_khan
  • 709
  • 7
  • 16

0 Answers0