首页 > 解决方案 > 将 ajax POST 与 codeigniter 一起使用的问题

问题描述

我正在做一个类似于票务系统的项目,有时它需要一些很长的答案,在答案区域我使用 CKEDITOR 而它正在键入的代理会使用 Json GET 自动将更改保存到数据库中,但是一些很长的答案是不工作,出现以下错误:

    Request-URI Too Long
The requested URL's length exceeds the capacity limit for this server.
Additionally, a 414 Request-URI Too Long error was encountered while trying to use an ErrorDocument to handle the request.

我发现这是由于使用 GET 引起的,我被建议将其更改为 POST 之后我没有那个错误,现在我得到 403 禁止

这是json代码

CKEDITOR.replace('consulta-body', {
  height: '300',
  on: {
      change: function( evt ) {
            for ( instance in CKEDITOR.instances ){
              CKEDITOR.instances[instance].updateElement();
            }
          var BodyText = $('#consulta-body').val();
          console.log( BodyText ); 

          var FormData = {};
          FormData['update'] = '1';
          FormData['id'] = <? echo $this->uri->segment(3);?>;
          FormData['name'] = 'previo';
             FormData['val'] = BodyText;
          $.ajax({
            dataType: 'json',
            type: 'POST',
            data: FormData,
            url: '<?echo base_url('consultas/ver/1212');?>',
            beforeSend: function(){},
            success: function(Response){}
          });
        }
  }
});

提前致谢

编辑:试过这个,控制台上没有任何错误,但它没有保存到数据库

CKEDITOR.replace('consulta-body', {
  height: '300',
  on: {
      change: function( evt ) {
            for ( instance in CKEDITOR.instances ){
              CKEDITOR.instances[instance].updateElement();
            }
          var BodyText = $('#consulta-body').val();
          console.log( BodyText ); 

        var FormData = {};
          FormData['<?php echo $this->security->get_csrf_token_name(); ?>']
                       = '<?php echo $this->security->get_csrf_hash(); ?>';
          FormData['update'] = '1';
          FormData['id'] = <? echo $this->uri->segment(3);?>;
          FormData['name'] = 'previo';
             FormData['val'] = BodyText;
          $.ajax({
            dataType: 'json',
            type: 'POST',
            data: FormData,
            url: '<?echo base_url('consultas/ver/1212');?>',
            beforeSend: function(){},
            success: function(Response){}
          });
        }
  }
});

标签: javascriptphpjsoncodeigniter

解决方案


我相信您会得到它,因为这<?echo base_url('consultas/ver/1212');?>会产生错误。

你可以这样写:

<?php echo base_url('consultas/ver/1212'); ?>

或者像这样:

<?= base_url('consultas/ver/1212'); ?>

见: https ://www.codeigniter.com/user_guide/general/alternative_php.html

请注意此错误在您的代码中出现了几次,因此请修改所有错误的块


推荐阅读