Skip to content

not work with ajax post #5

@ericfavoni

Description

@ericfavoni

I check for send form with jquery ajax $.post method. but csrf not work for me and show always invalid CSRF token.
JS:

                    jQuery(document).ready(function($) {
                         $('#myform').validate({

                        rules: {
                            name: {
                                required: true,
                                rangelength: [4, 20],
                            },
                            email: {
                                required: true,
                                email: true,
                            },
                            message: {
                                required: true
                            }
                        },

                        submitHandler: function(form) {

                            if (grecaptcha.getResponse() == '') {

                                $('#reCaptchaError').html('<p>Recaptcha error</p>');
                            } else {

                                $('#reCaptchaError').hide();

                                $("#ajax-form-msg1").html("<img src='<?php echo RELATIVE_PATH. '/templates/'. TEMPLATENAME; ?>'/img/loading.gif'/>");

                                //  var formData = $("#myform").serialize();  //or
                                var formData = $("#myform").serializeArray();

                                var URL = $("#myform").attr('action');
                                cache: false,
                                    $.post(URL,
                                        formData,
                                        function(data, textStatus, jqXHR) {
                                            if (data == "yes") {
                                                $("#ajax-form-msg1").html('<div class="alert alert-success">' + data + '</div>');
                                                $("#form-content").modal('show');
                                                $(".contact-form").slideUp();
                                            } else {

                                                $("#ajax-form-msg1").html('' + data + '');


                                            }

                                        }).fail(function(jqXHR, textStatus, errorThrown) {
                                        $("#ajax-form-msg1").html('<div class="alert alert-danger">AJAX Request Failed<br/> textStatus=' + textStatus + ', errorThrown=' + errorThrown + '</code></pre>');
                                    });


                            }
                        }
                    });
                });`

HTML:

`

SEND `

remote comment.php

     <?php
       session_start();
       $abspath = preg_replace('/\\\/', '/', dirname(dirname(__FILE__)));

       if (!file_exists($abspath . '/config.php'))
       {
       header('Location:404.html');
       }

       require $abspath . '/config.php';

       require ABSPATH . '/class/nocsrf.php';

       try
       {
        NoCSRF::check('csrf_token', $_POST, true, 60 * 10, true);
       }

       catch(Exception $e)
       {
       echo $e->getMessage();
       }

u check your class for ajax post form data ?! how do work your class for ajax post data?!

Thanks for gr8 class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions