Welcome Guest, Not a member yet? Register   Sign In
CI3 file upload via ajax with csrf
#3

(This post was last modified: 06-04-2020, 06:09 AM by Goddard.)

(06-03-2020, 07:56 AM)dave friend Wrote: Assuming that the csrf token is included in the data sent to the server a failure to accept the POST is usually because the csrf has not been refreshed after a previous POST. Let me try to make that more clear. You only get to make one POST request using any given csrf token.

Without seeing your code it's impossible to offer concrete advice.


It works fine when I use the jquery serialize.  If I use this


Code:
            var formData = new FormData(this);
            var file_data = $('#document').prop('files')[0];
          formData.append('file', file_data);
          formData.append('<?php echo $this->security->get_csrf_token_name(); ?>', '<?php echo $this->security->get_csrf_hash(); ?>');

         


It doesn't work.


Here is a complete ajax request.


;
Code:
        $('form#documents-form').submit(function(e) {
            e.preventDefault();
            var formData = new FormData(this);
            var file_data = $('#document').prop('files')[0];
          formData.append('file', file_data);
          formData.append('<?php echo $this->security->get_csrf_token_name(); ?>', '<?php echo $this->security->get_csrf_hash(); ?>');
         
         
            $('.should-disable-add-document-modal-button').attr('disabled', true);

            if(!$("#documents-form").parsley().validate()) {
                return;
            }

            <?php if($document->document_id): ?>
                $.ajax({
                    type: 'POST',
                    url: '/documents/update/<?= $document->document_id; ?><?=$entity_type == 'client' ? '/' . $entity_id : ''; ?>',
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        $('.should-disable-add-document-modal-button').attr('disabled', false);
                        $('#add-document-modal').modal('hide');
                        refreshDocuments();
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                        console.log(errorThrown);
                    }
                });

            <?php endif; ?>
            formmodified=0

        });
Reply


Messages In This Thread
CI3 file upload via ajax with csrf - by Goddard - 06-03-2020, 07:43 AM
RE: CI3 file upload via ajax with csrf - by Goddard - 06-03-2020, 08:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB