douna1892 2013-12-30 07:47
浏览 56

我的Ajax代码无法在codeigniter中发布数据库中的数据

I have the following javascript code that takes a text from a text area and passes it over to a controller which then updates the database. The controller then gets the same text from the db using a model and passes it over to the javascript code to be displayed on a given div of the webpage without refreshing the page. The controller just works fine and returns the text after successful update to the db but my problem is, the ajax code does not post the same message on the given div, it returns the alert error. I dont know how to fix this, pliz help me.

<script type="text/javascript">
        $(function() {

            $('#submit').click(function() {

                //get input data as a array
                var post_data = {
                    'message': $("#message").val(),
                    '<?php echo $this->security->get_csrf_token_name(); ?>': '<?php echo $this->security->get_csrf_hash(); ?>'
                };

                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>index.php/fb/insertByajax",
                    data: post_data,
                    success: function(response) {
                        debugger;
                        // return success message to the id='result' position
                        $("#result").html(response);
                    },
                  error: function() {alert("oops..."); }
                });

            });

        });
    </script>
  • 写回答

1条回答 默认 最新

  • duanfu5239 2014-01-24 20:19
    关注

    First, understand that base_url() is different of site_url() exactly because the second one appends /index.php or $config['index_page'] value. So you want:

    ...

    url: "<?php echo site_url('fb/insertByajax'); ?>",

    Also check in network session of Chrome's developer tools if there is any bad response from your page.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行