控制器方法无法识别joomla令牌

时间:2013-04-18 20:23:57

标签: ajax joomla token

我正在尝试在ajax帖子上设置一个令牌,但是没有得到控制器方法的识别。 javascrip看起来如下

jQuery(document).ready(function() {
    jQuery('#source').change(function() {
        jQuery('#fileupload').addClass('fileupload-processing');
        var data = jQuery('#source option:selected').val();
        jQuery.post('index.php', {
            'option': 'com_tieraerzte',
            'task': 'parser.importColumns',
            'tmpl': 'component',
            'token':'<?php echo JUtility::getToken()?>',
            'app': data,
            'dataType': 'html',
        }, function(result) {
            jQuery('td.add_column').html(result);
            jQuery('button#parse.btn').show();
            //edit the result here
            return;
        });
    });

生成并发布令牌

在控制器中我检查了toke的存在但是却抛出了无效的令牌

控制器检查toke

 JRequest::checkToken('request') or jexit( 'Invalid Token' );

2 个答案:

答案 0 :(得分:9)

你几乎就在那里,它只是有点混乱。 Joomla!表单令牌生成并作为输入名称提交,值为1.因此,令牌在您的表单中如下所示:

<input type="hidden" name="1LKJFO39UKSDJF1LO8UFANL34R" value="1" />

考虑到这一点,在通过AJAX提交时,您需要将参数名称设置为您的令牌名称,值为1.我通过使用jQuery('selector').serialize()方法完成类似的操作:

Joomla.autoSave = function () {
  jQuery.ajax({
    url: "index.php?option=com_gazebos&task=product.apply&tmpl=component",
    type: "POST",
    data: jQuery("#product-form").serialize(),
    success: function (data) {
      console.log("autosaved");
    }
  });
};

执行此操作会提取所有表单数据(包括隐藏输入中的表单标记)并将其格式化为查询字符串,然后将其与请求一起发送。但是,在我看来,您可能不想这样做,而您实际上只想提交一些数据,而不是整个表单。所以,让我们重新编写你的代码以获得预期的效果:

/**
 * First, let's alias $ to jQuery inside this block,
 * then setup a var to hold our select list dom object.
 */
jQuery(document).ready(function ($) {
    var sourceSelect = $('#source');

    sourceSelect.change(function () {
        $('#fileupload').addClass('fileupload-processing');

        /**
         * Use the token as a parameter name and 1 as the value,
         * and move the dataType param to after the success method.
         */
        $.post('index.php',
          {
            'option':   'com_tieraerzte',
            'task':     'parser.importColumns',
            'tmpl':     'component',
            'app':      sourceSelect.val(),
            '<?php echo JSession::getFormToken()?>': 1
          },
          function(result) {
            $('td.add_column').html(result);
            $('button#parse.btn').show();
            //edit the result here
            return;
          },
          'html'
        );
    });
});

最后,此代码假设您在view.html.phpviews/parser/tmpl/default.php中拥有此js代码。如果你将它放在一个单独的.js文件中,那么你的php代码就不会执行并给你提供令牌。

答案 1 :(得分:0)

在你的ajax调用方法中,使用url as:

$.ajax({
              url: '/index.php?option=com_itemreview&task=item.userReviewVote&<?php echo JSession::getFormToken(); ?>=1',
              type: 'post',
              data: {'data': submitvalue},
              dataType: 'json',
              success: function(response) {
              }
});

有关详细信息,请参阅此处:

http://joomlabuzz.com/blog/27-preventing-cross-site-request-forgery-in-joomla

https://docs.joomla.org/How_to_add_CSRF_anti-spoofing_to_forms

相关问题