2013-12-09 15 views
0

我使用themeforst的管理模板,现在我需要通过ajax将数据发送到页面。当我收到它时,我将发送所有的“wysiwyg”编辑器页面 - 它不适用于我。那么如何正确解析它?CodeIgniter - 如何通过ajax传递wysiwyg编辑器

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']); 

我应该得到:wsiwyg editor 我却越来越:enter image description here

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

回答

1

T他的问题来自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 
    }); 
});