You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

上传HTML至服务器时使用LZ-String出现内容匹配异常如何解决?

问题描述

我写了一个HTML/JS测试页面,会将LZ压缩后的HTML表格内容上传至PHP测试脚本,脚本再把上传内容返回至浏览器。当前代码存在如下问题:

  • 使用当前HTML代码时,返回内容与发送内容不匹配
  • 但只要对HTML做微小修改(比如将“HELLO”改为“HEADING”,或移除thead标签),功能即可正常运行
  • 无论比较结果为true还是false,字符表面完全一致,用差异检查工具也无法验证出区别
  • 由于追求更优压缩效果,暂未测试LZString.compressToBase64()方法
测试代码

HTML/JS代码

<html>
    <head>
        <title>LZ-String Test</title>
        
        <style>
            table {border-collapse: collapse;}
            th, td {border: 1px solid black;}
        </style>

        <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script type="text/javascript" language="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lz-string/1.4.4/lz-string.min.js"></script>
        <script>
            var content = `
                <table>
                    <thead>
                        <tr>
                            <th>HELLO</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>10</td>
                        </tr>
                    </tbody>
                </table>
            `;
        
            $(document).ready(function() {
                var formData = new FormData();
                var compressedContent = LZString.compress(content);
                formData.append("fileContent", compressedContent);
            
                $.ajax({
                    type: "POST",
                    url: "test.php",
                    data: formData,
                    cache: false,
                    processData: false,
                    contentType: false
                    
                }).done(function(data) {
                    var result = JSON.parse(data);
                    if (result.result == "success") {
                        $("body").append("<p>This is the html I compressed:<br>" + content + "</p>");
                        $("body").append("<p>This is what I sent to the server:<br>" + result.received + ".</p>");
                        $("body").append("<p>This is what I received from the server:<br>" + result.received + ".</p>");
                        $("body").append("<p>Using ===, what was sent matches what was returned is " + (compressedContent === result.received) + ".</p>");
                        $("body").append("<p>Using localCompare, what was sent matches what was returned is " + (compressedContent.localeCompare(result.received) === 0) + ".</p>");
                    }
                });
            });
        </script>
    </head>
    <body></body>
</html>

PHP代码

<?php
    $content = $_REQUEST["fileContent"];
    echo json_encode(array ("result" => "success", "received" => $content));
?>

内容的提问来源于stack exchange,提问作者Alan M.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 08:54:24