调用PHP精细差异结果的JQuery AJAX给出了错误的值

时间:2015-03-05 04:50:04

标签: php jquery ajax

我在CI中使用来自https://github.com/gorhill/PHP-FineDiff的精细差异。当我在没有JQuery(和AJAX)的情况下使用它时,结果很好。 (我真的没有编辑任何东西) enter image description here

但是当我使用JQuery将它加载到div中时,它会产生不同的结果 enter image description here 这是我在JQuery中使用的代码块: var id = $(document).find('title').text(); var dataString = 'record_type='+recordtype+'&content='+content+'&id='+id+'&series_number='+number; var prev = $.ajax({ url: getBaseUrl()+'/index.php/edititems/doc/'+id, type: 'post', data: dataString, async: false }).responseText; $('.placeholder').html(prev);

这是CI代码(使用相同的控制器和功能):

$data['doc'] = $this->LoadDb->preview($this->uri->segment(3));
    $this->load->library('ciqrcode');

    $params['data'] = base_url().'assets/pdfs/'.$data['doc']['record_id'].'.pdf';
    $params['level'] = 'H';
    $params['size'] = 10;
    $params['savename'] = FCPATH.'tes.png';
    $this->ciqrcode->generate($params);

    $data['doc']['qr'] = '<img src="tes.png" style="height:90px;"/>';


    $from_text = $data['doc']['content'];

    require_once(APPPATH.'libraries/finediff.php');
    $cache_lo_water_mark = 900;
    $cache_hi_water_mark = 1100;
    $compressed_serialized_filename_extension = '.store.gz';

    $granularity = 0;
    // $from_text = '';
    $to_text = '';
    $diff_opcodes = '';
    $diff_opcodes_len = 0;
    $data_key = '';

    $start_time = gettimeofday(true);

    $granularity = max(min(intval(2),3),0);     

    if ( !empty($this->input->post('content')) ) {
        $to_text = $this->input->post('content');
    }
    // limit input
    $from_text = substr($from_text, 0, 1024*100);
    $to_text = substr($to_text, 0, 1024*100);

    // ensure input is suitable for diff
    $from_text = mb_convert_encoding($from_text, 'HTML-ENTITIES', 'UTF-8');
    $to_text = mb_convert_encoding($to_text, 'HTML-ENTITIES', 'UTF-8');

    $granularityStacks = array(
        FineDiff::$paragraphGranularity,
        FineDiff::$sentenceGranularity,
        FineDiff::$wordGranularity,
        FineDiff::$characterGranularity
        );
    $diff_opcodes = FineDiff::getDiffOpcodes($from_text, $to_text, $granularityStacks[$granularity]);
    $diff_opcodes_len = strlen($diff_opcodes);
    $exec_time = gettimeofday(true) - $start_time;
    if ( $diff_opcodes_len ) {
        $data_key = sha1(serialize(array('granularity' => $granularity, 'from_text' => $from_text, 'diff_opcodes' => $diff_opcodes)));
        $filename = "{$data_key}{$compressed_serialized_filename_extension}";
        if ( !file_exists("./cache/{$filename}") ) {
            // purge cache if too many files
            if ( !(time() % 100) ) {
                $files = glob("./cache/*{$compressed_serialized_filename_extension}");
                $num_files = $files ? count($files) : 0;
                if ( $num_files > $cache_hi_water_mark ) {
                    $sorted_files = array();
                    foreach ( $files as $file ) {
                        $sorted_files[strval(@filemtime("./cache/{$file}")).$file] = $file;
                        }
                    ksort($sorted_files);
                    foreach ( $sorted_files as $file ) {
                        @unlink("./cache/{$file}");
                        $num_files -= 1;
                        if ( $num_files < $cache_lo_water_mark ) {
                            break;
                            }
                        }
                    }
                }
            // save diff in cache
            $data_to_serialize = array(
                'granularity' => $granularity,
                'from_text' => $from_text,
                'diff_opcodes' => $diff_opcodes,
                'data_key' => $data_key,
                );
            $serialized_data = serialize($data_to_serialize);
            @file_put_contents("./cache/{$filename}", gzcompress($serialized_data));
            @chmod("./cache/{$filename}", 0666);
            }
        }       

        $rendered_diff = FineDiff::renderDiffToHTMLFromOpcodes($from_text, $diff_opcodes);
        $from_len = strlen($from_text);
        $to_len = strlen($to_text);
        echo "<i>Original Text:<br></i>".$from_text."<br>";
        echo $data['doc']['content'] = "<i>Edited Text<br></i>".htmlspecialchars_decode($rendered_diff);
        $content = preg_replace('/<del\b[^>]*>(.*?)<\/del>/i', "", $rendered_diff);
        echo '<i>New Text:</i>'.htmlspecialchars_decode(preg_replace('/<\/?ins[^>]*\>/i', "", $content));

有人遇到过同样的问题吗?请帮忙。谢谢。

1 个答案:

答案 0 :(得分:0)

我编辑了我的剧本,现在它运作良好。我没有通过AJAX传递dataString,而是使用data: $('form').serialize()传递表单数据。