【发布时间】:2015-08-06 15:18:27
【问题描述】:
我想使用 jquery ajax 提交我的表单,没关系,但只是我第一次点击提交,这是我的代码。
<!-- input code from the form -->
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>" />
<!-- js code -->
$.ajax({
url: ajaxurls.ask,
type: 'POST',
data: formData,
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData:false,
success: function (html) {
var data = jQuery.parseJSON(html);
if(data.status == 'ok') {
$('.row_question_form').after(data.response.html);
$('.question_' + data.response.question[0].question_id).hide().fadeIn();
$('#question_form').val('');
$('#thumbnails').empty();
$('#ask_question_messages').empty();
$('#ask_question_messages').html(data.message);
}else if(data.status == 'error'){
$('#ask_question_messages').empty();
$('#ask_question_messages').html(data.message);
}
}
});
您可能会说设置$config['csrf_regenerate'] = FALSE;,但在这种情况下,有人可以使用这样的应用程序在我的数据库中创建数百条记录:
【问题讨论】:
-
第一次可以提交表单吗?
-
是的,当第一次调用控制器时,csrf 会发生变化,因此第二次无法正常工作。我必须刷新页面才能再次工作。
-
CSRF 令牌是一次性使用的。
-
是的,但是如何将 CSRF 与 ajax 一起使用?我可以多次使用相同的表单而无需重新加载页面(但是当我发出 ajax 请求时,csrf 令牌会更改,下次表单将不再起作用)
标签: php jquery codeigniter