我正在将此表单构建器(shlomnissan/formbuilder)集成到Codeigniter中,并在尝试保存表单时收到500服务器错误。
我正在传递CSRF令牌名称和值(我已将这些记录到控制台以确认它们存在)
通常我会使用PHP来回显ajax表单帖子的值,但是javascript是在一个单独的JS文件中。
调用formbuilder函数的代码如下:
$('#form-builder').formBuilder({
load_url: '/formbuilder/get_form_template',
save_url: '/formbuilder/save_form',
csrf_name: '<?php echo $this->security->get_csrf_token_name(); ?>',
csrf_token: '<?php echo $this->security->get_csrf_hash(); ?>'
});
我已经在formBuilder.js文件中更改了代码,如下所示,但这给了我一个“无法读取属性'调用'未定义”错误:
$('#save').click(function(e){
var form_data = serialize();
var csrf_name = settings.csrf_name;
var csrf_token = settings.csrf_token;
$.ajax({
type: "POST",
url: settings.save_url,
data: {formData: form_data, csrf_name: csrf_token},
success: function () {
settings.onSaveForm.call();
}
});
我还设置了在序列化之前包含在form_data变量中的值:
var serialize = function() {
var formData = {};
formData['title'] = $('#form-title').val();
formData['description'] = $('#form-description').val();
var csrf_token = $.cookie('csrf_cookie_name');
formData['csrf_token_name'] = csrf_token;
为简洁起见,从上面删除了一些代码。
提交表单后,我收到500内部服务器错误;传递的表单数据如下:
{"title":"My Form","description":"This is my form. Please fill it out. It's awesome!","csrf_token_name":"bcf43ac1ca40e9d0fa8d1c6d6da9f34b","fields":[{"title":"First Name","type":"element-single-line-text","required":true,"position":1},{"title":"Last Name","type":"element-single-line-text","required":true,"position":2},{"title":"Email","type":"element-single-line-text","required":true,"position":3}]}
如果您需要查看更多代码,请告诉我们。 任何帮助表示感谢,谢谢。