我试图使用ajax将文本从一个文本区域发布到php页面,以回显到另一个文本区域。当我这样做,它工作约5个字符,然后完全停止。我尝试了编码和不编码似乎没有什么区别,因为我只是尝试简单的计划文本,如"ddddddddddd“。
在example.html中
<!DOCTYPE html>
<html>
<body>
<style>
.center{
margin: 0 auto;
margin-left: auto;
margin-right: auto;
text-align:center;
}
</style>
<div class="center">
<textarea   onkeyup="changed()" onkeydown="changed()" onkeypress="changed()"  id="b1" rows="30" cols="81">
</textarea>
<textarea id="b2" rows="30" cols="81">
</textarea>
</div>
</body>
</html>
<script>
function changed(){
var text =document.getElementById("b1").value;
if(!text){
text ="";
}
     var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
                document.getElementById('b2').value =xhr.responseText;
        }
    }
    xhr.open('POST', 'echo.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("text=" + text);
}
</script>在echo.php中
<?php 
 $mytext = $_POST["text"];
 echo $mytext;
?>我看过几个类似的帖子,但没有一个能解决我的问题。我不是在用表格。我必须去吗?Textarea not POSTing with form
发布于 2015-07-31 03:37:35
我不知道您想做什么,但是如果您将脚本更改为以下内容,则可以以较少的需求实现相同的输出:
<script>
function changed(){
    var text =document.getElementById("b1");
    if(!text){
       text ="";
    }
    else
    {
        text=text.value;
    }
    var text2 =document.getElementById("b2");
    if(!text2)
    {
       text2.value=text;
    }
}
</script>希望能帮上忙。
编辑
现在发现了错误,似乎您是在随机返回ajax请求。
现在,当第二个ajax首先响应时会发生错误,所以第二个文本区域的值最初将是'asdf‘,但在第一个ajax响应时,它将覆盖当前值到'asd’。
要解决这个问题,请将您的脚本更改为:
    <script>
    var pending_request=false;
    function changed(){
    if(pending_request==false)
    {
        var text =document.getElementById("b1").value;
        if(!text){
            text ="";
        }
        var xhr;
        if (window.XMLHttpRequest) {
            xhr = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            xhr = new ActiveXObject("Msxml2.XMLHTTP");
        }
        else {
            throw new Error("Ajax is not supported by this browser");
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                document.getElementById('b2').value =xhr.responseText;
                pending_request=false;
            }
        }
        pending_request=true;
        xhr.open('POST', 'echo.php');
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send("text=" + text);
    }
    else
    {
      setTimeout(function(){ changed(); }, 3000);
    }
   }
   </script>demo here
这个想法是在发送另一个:D之前等待挂起的ajax请求的响应。
对不起,我的英语很差。
发布于 2015-07-31 22:23:05
我做了VMcreator建议的修改,并看到了很大的改进,但它仍然会“崩溃”。防止接收任何其他字符。我在他的演示上试过同样的步骤。效果很好。我决定尝试在另一个托管提供商上托管我的页面和php脚本,它成功了!
我打电话给我的主机提供商(godaddy),他们说由于服务器接收来自同一个ip的快速请求,所以被他们的垃圾邮件过滤器拒绝。虽然我的ip没有被阻止,但我的许多请求都是这样,导致了更糟糕的种族状况。
即时解决方案-交换机主机提供商
长期解决方案-(尚未发生)更改安全设置以接受请求。
https://stackoverflow.com/questions/31732585
复制相似问题