dongzhang0418 2015-12-22 20:55
浏览 83

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

i am working with ckeditor.I used ajax to send the content of the ckeditor using post method.My plan is to show the response in a div .But in response i get the html for the whole page enclodes by html tag instead of only the editor's content.What is going on here? How can i get only the editor's content

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条回答 默认 最新

  • 普通网友 2015-12-22 21:35
    关注

    When You use echo, the response is sent and then the html is sent. If you want to stop after echo, use die();:

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

    Also, you should check that content is set:

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

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接