简体   繁体   English

Javascript / AJAX脚本检查PHP页面是否输出1

[英]Javascript/AJAX script to check if PHP page outputs 1

I am trying to create an instant messaging site. 我正在尝试创建一个即时消息网站。 Every ten seconds I want a javascript/ajax script to check if there are new messages. 我希望每隔十秒钟使用javascript / ajax脚本检查是否有新消息。 I thought that I could have a php page output 1 if there are. 我以为我可以有一个php页面输出1(如果有)。 I have done the php coding, but can not get the javascript side to work. 我已经完成了php编码,但是无法使javascript端正常工作。 I have tried using $.get and am having a bit of trouble. 我尝试使用$ .get,并且遇到了一些麻烦。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is my ajax code: 这是我的ajax代码:

var check;
function checkForMessages() {
    $.get("/checkmsg.php", function(data) {
        if(data == 1) {
            alert("There is a new message");
        }
    });
}

check = setInterval(checkForMessages, 10000);

My php code always outputs 1 for testing purposes 我的php代码始终输出1进行测试

I'm guessing at the end of your php code you have this: 我猜在您的php代码的末尾有这个:

echo '1';

For your JS i suggest $.post(): 对于您的JS,我建议$ .post():

jQuery.post( '/checkmsg.php', function(data) { 
    if (data.search('1') !== -1) {
        alert("There is a new message");
    } else {
        alert("Something went wrong.");
    }
});

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

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