How to define CSRF token in ajax call in Cakephp 3. Also How CSRF can be off for some ajax requests

后端 未结 3 1053
半阙折子戏
半阙折子戏 2021-01-18 06:46

In Cakephp3 when the Csrf component is enabled. How I can use it in ajax call. In this beforeSend parameter of ajax csrf token is set in header. What is the val

3条回答
  •  鱼传尺愫
    2021-01-18 06:52

    Every form has a hidden _csrfToken field that's automatically added when you have enabled the Csrf component. Now you can easily get the token of this field by jquery like $('[name="_csrfToken"]').val().

    A ajax call will look like this:

    $.ajax({
       url: 'someUrl',
       headers : {
          'X-CSRF-Token': $('[name="_csrfToken"]').val()
       },
       type: 'post',
       ...
    });
    

提交回复
热议问题