ajax响应包含整个页面的html而不是文本编辑器的内容

时间:2015-12-22 20:55:12

标签: javascript php html ajax ckeditor

我正在使用ckeditor.I使用ajax使用post方法发送ckeditor的内容。我的计划是在div中显示响应。但作为响应我得到整个页面的html被html标签包围而不是只有编辑的内容。这里发生了什么?我怎样才能获得编辑的内容

enter image description here

<?php  
if(isset($_POST) && !empty($_POST)){
echo $_POST['content'];
}
?>

<html>
<head>
  <script  src='ckeditor/ckeditor.js'></script>
</head>
<body>
 <form action='test.php' method='post'>
            <textarea name="editor1" id="editor1" rows="10" cols="80">
                This is my textarea to be replaced with CKEditor.
            </textarea>
            <input type='button' value='submit' onClick='lol(event);'>
    </form>
    <div id='content' style='border:2px solid black;'>

    </div>
            <script>

CKEDITOR.replace( 'editor1');
function lol(event){
    var xhr=new XMLHttpRequest();

    xhr.onreadystatechange=function(){
       if(xhr.readyState==4 && xhr.status==200){
           //document.getElementById('content').innerHTML=xhr.response;  
           alert(xhr.responseText);
       }
    }
    var value=CKEDITOR.instances.editor1.getData();
    var params = "content="+value;
    xhr.open('POST','test.php',true);
    //xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(params);
    //alert(value);
}

            </script>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

当您使用echo时,会发送响应,然后发送html。如果您想在echo之后停止,请使用die();

<?php  
  if(isset($_POST) && !empty($_POST)) {
    die($_POST['content']);
  }
?>

此外,您应该检查内容是否已设置:

<?php  
  if(isset($_POST) && !empty($_POST) && isset($_POST['content'])) {
    die($_POST['content']);
  }
?>