Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Teranode's avatar

TokenMismatchException Ajax Help

I'm trying to have ckeditor inline functioning but i'm running into a problem. This is the ckeditor editable content

                <div id="{!! $user->name !!}-editable" class="about"
                @if(auth()->check())
                @if (auth()->user()->id == $user->id)
                 contenteditable="true"
                @endif
                @endif
                 >
                  {!! $user->about_me !!}
                </div>

this is the ajax to handle the edits:

    CKEDITOR.disableAutoInline = true;
    CKEDITOR.inline( '{!! $user->name !!}-editable', {
        on: {
            blur: function( event ) {
              var params = {
                  body_text: event.editor.getData()
              };

              jQuery.ajax({
                  url: '/users/{!! $user->name !!}',
                  global: false,
                  type: "PUT",
                  dataType: "text json",
                  data: params,
                  success: function(result) {
                      console.log(result);
                  }
              });

            }
        }
    });

What do i need to put in the ajax or the div for it to work?

0 likes
10 replies
Ricardo's avatar
var params = {
    body_text: event.editor.getData(),
    _token: <?php echo csrf_token(); ?>
};
Teranode's avatar

Thats the problem, i did try that before however it cant identify the token because there isnt a form to submit, im using ckeditor's inline editor, so there is no form submit, just a call to submit to the server. And it targetting the Div I provided.

Ricardo's avatar

Umm, the code showed in my answer is part of your code. I just added a _token field, and you does not need a form to do that.

gwp's avatar

In your ajax code, make sure you're grabbing the _token value from the DOM and pass it through.

Ricardo's avatar
Ricardo
Best Answer
Level 48
var params = {
    body_text: event.editor.getData(),
    _token: '<?php echo csrf_token(); ?>'
};

And it is not taken from the DOM :)

jeffer8a's avatar

you can avoid TokenMismatchException by editing your app/Http/Middleware/VerifyCsrfToken.php.

gwp's avatar

Maybe I'm miss interpreting the question. Apologies.

Ricardo's avatar

no worries :)

You assume that the token was set some where in the page, but that is not always true.

Please or to participate in this conversation.