上传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.
相关产品推荐
相关产品推荐

