Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax自动更新.txt_Php_Jquery_Html_Ajax - Fatal编程技术网

Php 使用ajax自动更新.txt

Php 使用ajax自动更新.txt,php,jquery,html,ajax,Php,Jquery,Html,Ajax,由于我是网络编程新手,我有一些问题。 我想实现一个显示在index.php上的非常基本的聊天 它基本上是一个*.txt文件,应该每秒自动重新加载一次 我现在的代码: src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js" $(文档).ready(函数(){ console.log(“准备就绪!”); }); 函数getLog(){ $.ajax({ url:'chat.txt', 数据类型:“文本”, 成功:函数

由于我是网络编程新手,我有一些问题。 我想实现一个显示在index.php上的非常基本的聊天

它基本上是一个*.txt文件,应该每秒自动重新加载一次

我现在的代码:


src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"
$(文档).ready(函数(){
console.log(“准备就绪!”);
});
函数getLog(){
$.ajax({
url:'chat.txt',
数据类型:“文本”,
成功:函数(文本){
$(“#聊天”).text(text);
setTimeout(getLog,1000);//每秒刷新一次
}
})
}
getLog();
(...)
Soloranking.de
聊天
添加

如果仍然不起作用,则从cdn添加jquery库

希望这对您有所帮助。

为jquery运行添加此库


您的$不正常。

属性
src
应位于
script
标记内。此外,所有代码都应位于
$(document).ready()函数中

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

<script>
    $( document ).ready(function() {
       function getLog() {
           $.ajax({
               url: 'chat.txt',
               dataType: 'text',
               success: function(text) {
                   $("#chat").text(text);
                   setTimeout(getLog, 1000); // refresh every second
               }
           })
        }

    getLog(); 
    });
</script>


<div class="panel panel-default">
    <div class="panel-heading">
        <strong>Soloranking.de Chat</strong>
    </div>
    <div id="chat"></div>
    </div>
</div>

$(文档).ready(函数(){
函数getLog(){
$.ajax({
url:'chat.txt',
数据类型:“文本”,
成功:函数(文本){
$(“#聊天”).text(text);
setTimeout(getLog,1000);//每秒刷新一次
}
})
}
getLog();
});
Soloranking.de Chat

也许可以将方法参数放在Ajax请求中

您还没有告诉我们您的问题:POh哦,它不起作用:D我没有显示文本字段:D检查控制台是否有错误。另外,您是如何写入文本文件的?控制台说:“uncaughtreferenceerror:$未定义在(索引):7”添加了库,不再有控制台错误。但根据我随后的ajax教程,我确实将其定义为o.ONo问题;)很高兴我能帮忙。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

<script>
    $( document ).ready(function() {
       function getLog() {
           $.ajax({
               url: 'chat.txt',
               dataType: 'text',
               success: function(text) {
                   $("#chat").text(text);
                   setTimeout(getLog, 1000); // refresh every second
               }
           })
        }

    getLog(); 
    });
</script>


<div class="panel panel-default">
    <div class="panel-heading">
        <strong>Soloranking.de Chat</strong>
    </div>
    <div id="chat"></div>
    </div>
</div>