0

I'm using admin template from themeforst and now I need to send data into page via ajax. I'm sending all page where is "wysiwyg" editor inside adn when I receive it - it doesn't work for me. So how to parse it correctly?

JS:

function editThis(id) {
    $("#failed_msg").fadeOut(100);
    $("#success_msg").fadeOut(100);

    $.ajax({
        url: "admin/news_validation/editNew",
        type: 'POST',
        dataType: 'JSON',
        data: {
            new_id: id
        },
        success: function (data) {
            if (data.failed) {
                $("#failed_msg").fadeIn(600).find('i').html(data.failed);
            } else if (data.success) {
                $("#main_block_parsing").fadeOut(600, function () {
                    $("#main_block_parsing").remove();
                    $("#edit_new_parsing").fadeIn(800, function () {
                        $("#edit_new_parsing").html(data.page);
                    });
                });
            }
        },
        error: function (e) {
            console.log(e.message);
        }
    });

PHP:

public function editNew(){
    $data = array('id' => $this->input->post('new_id'));
    $report = array();

    if(!$data['id'] || !is_numeric($data['id']) || $this->news_model->checkNewExsists($data['id']) == FALSE){
        $report['errors'] = array('failed' => 'Such new does not exsists');
    } else {
        ob_start();
        $this->load->view('admin/pages/news/edit_new');
        $result = ob_get_clean();

        $report['errors'] = array('success' => 'The new was parsed successfully!', 'page' => $result);
    }

    echo json_encode($report['errors']);

what i should get: wsiwyg editor what I actually getting: enter image description here

in the second photo as you can see the "wysiwyg" is not working

1 Answers1

1

The problem is from codeigniters CSRF protection. It expects a token on each post request.

You can validate that this is the problem by going into config and turning off CSRF protection temporarily. The form should now work. Now you should turn it back on and send tokens with your ajax request:

You can read more here: Codeigniter ajax CSRF problem

The gist of the solution is you need to use codeigniters security class to add a token and hash to the data you post

I really like georjars solution here: https://stackoverflow.com/a/16140018/2062925

Make sure you give him credit if this works:

   <script>
     var csfrData = {};
     csfrData['<?php echo $this->security->get_csrf_token_name(); ?>']
                       = '<?php echo $this->security->get_csrf_hash(); ?>';
   </script>
   <!-- ... include other javascript files -->
  </body>
</html>


$(function() {
    // Attach csfr data token
    $.ajaxSetup({
       data: csfrData
    });
});
Community
  • 1
  • 1
David Duncan
  • 1,858
  • 17
  • 21