我正在尝试在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;
});
});
Run Code Online (Sandbox Code Playgroud)
令牌生成并发布
在控制器中,我检查toke的存在,但抛出无效令牌
控制器检查toke
JRequest::checkToken('request') or jexit( 'Invalid Token' );
Run Code Online (Sandbox Code Playgroud)
你几乎就在那里,它只是有点混乱.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");
}
});
};
Run Code Online (Sandbox Code Playgroud)
这样做会提取所有表单数据(包括隐藏输入中的表单标记)并将其格式化为查询字符串,然后将其与请求一起发送.但是,在我看来,您可能不想这样做,而您实际上只想提交一些数据,而不是整个表单.所以,让我们重新编写你的代码以获得所需的效果:
/**
* 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'
);
});
});
Run Code Online (Sandbox Code Playgroud)
最后,这段代码假设你在你的view.html.php或你的中有这个js代码views/parser/tmpl/default.php.如果你将它放在一个单独的.js文件中,那么你的php代码将不会执行并为你提供令牌.
| 归档时间: |
|
| 查看次数: |
2955 次 |
| 最近记录: |