Welcome Guest, Not a member yet? Register   Sign In
Ajax with CSRF causing a Double Post to controller
#4

(10-01-2018, 08:55 AM)dave friend Wrote: Is the HTML element starting the process a "submit" button? If so, and if the ajax call is made through javascript and the submit button is not prevented doing its default action (with JQuery event.preventDefault() for example) that might explain the process happening twice. Maybe?

That doesn't really explain why the state of CSRF protection makes a difference. There isn't any code in the CSRF checking process that changes the code execution path if the CSRF token is valid.

Hi Dave,

Thanks for the response. I am using the ev.preventDefault(). My Ajax is called from a button submit, at the end of a .on chain.

It actually look a lot like this, some console log items have been removed.


Code:
var request = $.ajax({
           method: 'POST',
           url: serviceUrl,
           data: { user: userName, email: emailAddr, csrf_cookie_name: cct, comment: userComment },
           dataType: 'json',
           cache: false
       });
       console.log("submitted already");

       request.done(function (resp) {
           $completetitle.html(resp.title);
           $completemessage.text(resp.message);

           buttonSubmit.attr('disabled', false);

           if (resp.status == 'true') {
               $('#myform')[0].reset();
           }

           $complete.foundation('open');
       });

       request.fail(function (resp) {
           $completetitle.html("Network Error");
           $completemessage.html("<p>Something went wrong. The message was NOT sent.</p><p>Please phone instead.</p>");

           $complete.foundation('open');
       });


The code seems to be fine, apart from the double call over at the server. The putting of data into the Modal boxes all works fine.
Reply


Messages In This Thread
RE: Ajax with CSRF causing a Double Post to controller - by Chroma - 10-01-2018, 12:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB