通过jQuery ajax发送的内容正在被切断

Ato*_*mix 2 html php ajax jquery

我试图通过jQuery的$ .ajax()方法发送html,然后将html转储到新的.html文件中,但内容因某种原因被切断了.

我已经设置了一个测试用例:

<html>

<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script >
$(document).ready(function(){
      var contents = $('html').html();
      var filename = "test/000.html";
      $.ajax({
         type: "POST",
         url: "/file.php",
         data: "content="+contents+"&fn="+filename, 
         dataType: "html",
         success: function(msg, ts, xh){
             console.log(msg);
             console.log(ts);
             console.log(xh);
             alert("success");
         },
         processData: false,
         error: function(X, textStatus, error){
             alert("Error saving... please try again now.")
         }
     });
});
</script>


</head>

<body>

<div id="rteContainer" class="dev">
  <div id="textEditor">
    <form>
    <textarea name="balh" id="balh" rows="25" cols="103"></textarea> 
    <input type="button" id="updateContent" value="Update Changes">
    <input type="button" id="closeEditor" value="Close Without Saving">
    </form>
  </div>
</div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

file.php文件是:

<?php
header("Content-type:text/html;charset:UTF-8");
$content = "<html>".stripslashes(urldecode($_POST["content"]))."</html>";
$dump_file = $_POST["fn"];
$fp = fopen($dump_file, 'w');
fclose($fp);
echo $content;
?>
Run Code Online (Sandbox Code Playgroud)

为什么会被切断?我猜这是编码问题,但我无法解决这个问题.

Ale*_*lex 9

html字符串contents必须在您之前进行url编码POST.Javascript提供了这个escape()功能,仅用于此目的.

传递$('html').html()escape()并将其分配到contents,像这样:

var contents = escape($('html').html());
Run Code Online (Sandbox Code Playgroud)