【问题标题】:CodeIgniter - how to pass wysiwyg editor via ajaxCodeIgniter - 如何通过 ajax 传递所见即所得的编辑器
【发布时间】:2013-12-09 14:34:09
【问题描述】:

我正在使用 themeforst 的管理模板,现在我需要通过 ajax 将数据发送到页面。当我收到它时,我正在发送所有页面,其中“所见即所得”编辑器在 adn 中 - 它对我不起作用。那么如何正确解析呢?

JS:

function editThis(id) {
    $("#failed_msg").fadeOut(100);
    $("#success_msg").fadeOut(100);

    $.ajax({
        url: "admin/news_validation/editNew",
        type: 'POST',
        dataType: 'JSON',
        data: {
            new_id: id
        },
        success: function (data) {
            if (data.failed) {
                $("#failed_msg").fadeIn(600).find('i').html(data.failed);
            } else if (data.success) {
                $("#main_block_parsing").fadeOut(600, function () {
                    $("#main_block_parsing").remove();
                    $("#edit_new_parsing").fadeIn(800, function () {
                        $("#edit_new_parsing").html(data.page);
                    });
                });
            }
        },
        error: function (e) {
            console.log(e.message);
        }
    });

PHP:

public function editNew(){
    $data = array('id' => $this->input->post('new_id'));
    $report = array();

    if(!$data['id'] || !is_numeric($data['id']) || $this->news_model->checkNewExsists($data['id']) == FALSE){
        $report['errors'] = array('failed' => 'Such new does not exsists');
    } else {
        ob_start();
        $this->load->view('admin/pages/news/edit_new');
        $result = ob_get_clean();

        $report['errors'] = array('success' => 'The new was parsed successfully!', 'page' => $result);
    }

    echo json_encode($report['errors']);

我应该得到什么: 我实际上得到了什么:

在第二张照片中,您可以看到“所见即所得”不起作用

【问题讨论】:

    标签: jquery ajax codeigniter wysiwyg


    【解决方案1】:

    问题出在 codeigniters CSRF 保护上。它期望每个发布请求都有一个令牌。

    您可以通过进入配置并暂时关闭 CSRF 保护来验证这是问题所在。该表格现在应该可以工作了。现在您应该重新打开它并使用您的 ajax 请求发送令牌:

    您可以在这里阅读更多内容:Codeigniter ajax CSRF problem

    解决方案的要点是您需要使用 codeigniters 安全类为您发布的数据添加令牌和哈希

    我真的很喜欢这里的 georjars 解决方案:https://stackoverflow.com/a/16140018/2062925

    如果可行,请确保您给予他信任:

       <script>
         var csfrData = {};
         csfrData['<?php echo $this->security->get_csrf_token_name(); ?>']
                           = '<?php echo $this->security->get_csrf_hash(); ?>';
       </script>
       <!-- ... include other javascript files -->
      </body>
    </html>
    
    
    $(function() {
        // Attach csfr data token
        $.ajaxSetup({
           data: csfrData
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多