我发现在GitHub issuse(german)
为Contao答案> 3.X起初做您的前端模板:
<script type="text/javascript">
var data = {};
data["REQUEST_TOKEN"] = "<?php echo REQUEST_TOKEN ?>";
$(document).ready(function(){
$("#trigger").click(function(event){
$.post(
'<?php echo \Contao\Environment::get('requestUri')?>',
data,
function(responseText) {
alert(responseText);
}
).fail(function(jqXhr, textStatus, errorThrown){ console.log(errorThrown)});
event.preventDefault();
});
});</script>
重要的是 - 数据[ “REQUEST_TOKEN”] - >如果您不添加它,POST请求将不会到达您的模块:
public function generate()
{
if ($_SERVER['REQUEST_METHOD']=="POST" && \Environment::get('isAjaxRequest')) {
$this->myGenerateAjax();
exit;
}
return parent::generate();
}
//do in frontend
protected function compile()
{
...
}
public function myGenerateAjax()
{
// Ajax Requests verarbeiten
if(\Environment::get('isAjaxRequest')) {
header('Content-Type: application/json; charset=UTF-8');
echo json_encode(array(1, 2, 3));
exit;
}
}
如果你想通过GET来完成ajax,你不需要reqest token,但是需要jquery funktion $ get();
不错的一..这肯定值得upvote ..这似乎工作在contao 3.5.17(最新版本稳定日期).. Thanx队友.. – DpEN
在响应有时我们得到204状态,因为缓存问题可以给有一个解决方案呢? –
切缓存?你在说什么缓存? – garyee