简体   繁体   English

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

[英]ajax response contains the html for the whole page instead of the content of text editor

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?我正在使用 ckeditor。我使用 ajax 通过 post 方法发送 ckeditor 的内容。我的计划是在 div 中显示响应。但是作为响应,我得到了整个页面的 html 由 html 标记而不是仅包含编辑的内容。这是怎么回事? How can i get only the editor's content我怎样才能只得到编辑器的内容

在此处输入图像描述

<?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>

When You use echo , the response is sent and then the html is sent. 当您使用echo ,将发送响应,然后发送html。 If you want to stop after echo , use die(); 如果要在echo后停止,请使用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']);
  }
?>

Try this solution by adding variable to Javascript variable which called params and this variable to check if already these items opened or not and prevent main items to not open more than 1 time even if u called your page by ajax, to be clear try l8ike that var params = "content="+value+"isopepend=1";通过向调用 params 和此变量的 Javascript 变量添加变量来尝试此解决方案,以检查这些项目是否已打开,并防止主要项目打开不超过 1 次,即使您通过 ajax 调用您的页面,要明确尝试 l8ike var params = "content="+value+"isopepend=1"; then use php if statement like if there is no required variable then print your main items and this will only happen one time when u will visit the page directly with no ajax request然后使用 php if 语句,如果没有必需的变量,然后打印您的主要项目,这只会发生一次,当您直接访问页面时没有 ajax 请求

<?PHP
    if(!$_POST['isopepend'])
    {
    echo "home elements";
    }
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM