Back to Jquery class

Method token

public static void
token
(mixed $name = 'csrf.token')
Auto set CSRF token to ajaxSetup so all jQuery ajax call will contains CSRF token.
Parameters
  • string $name The CSRF meta tag name.
Returns
  • void
Since
  • 3.8.0
-
  • \InvalidArgumentException
Class: Jquery
Project: Joomla

Method token - Source code

/**
 * Auto set CSRF token to ajaxSetup so all jQuery ajax call will contains CSRF token.
 *
 * @param   string  $name  The CSRF meta tag name.
 *
 * @return  void
 *
 * @throws  \InvalidArgumentException
 *
 * @since   3.8.0
 */
public static function token($name = 'csrf.token')
{
    // Only load once
    if (!empty(static::$loaded[__METHOD__][$name])) {
        return;
    }
    static::framework();
    HTMLHelper::_('form.csrf', $name);
    $doc = Factory::getDocument();
    $doc->addScriptDeclaration(<<<JS
;(function (\$) {
\t\$.ajaxSetup({
\t\theaders: {
\t\t\t'X-CSRF-Token': Joomla.getOptions('{$name}')
\t\t}
\t});
})(jQuery);
JS
);
    static::$loaded[__METHOD__][$name] = true;
}