0

我正在与 JS 和 PHP 进行某种聊天。

应用程序每 3 秒发出 GET xmlhttp 请求以获取消息,并且每 3 秒发出 POST 以设置 lastSeen 信息。

我不明白为什么 POST 调用有效而 GET 调用无效。

我已经检查了有关此论点的其他帖子并尝试过,但问题仍然存在。

谢谢

$(document).ready(function () {

        window.setInterval(stampa, 3000);

        $('#invia').click(function () {

            var inp = $('#input').val();

            var xmlhttp;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {

            }
            xmlhttp.open("POST", "http://ritapp.altervista.org/chatData.php", true);
            xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

            var sendData = "type=" + "post" + "&nome1=" + "CAVI" + "&pic1=" + "imm1" + "&nome2=" + "CLALTAR" + "&pic2=" + "imm2" + "&text=" + inp;

            xmlhttp.send(sendData);

        });

    });


    function stampa() {
        var chatData;
        downloadUrl("http://ritapp.altervista.org/chatData.php?type=get&nome1=" + "CAVI",
            function (data) {

                var xml = data.responseXML;

                chatData = xml.documentElement.getElementsByTagName("get");


                for (var i = 0; i < chatData.length; i++) {

                    var nome1 = chatData[i].getAttribute("nome1"); //0

                    var pic1 = chatData[i].getAttribute("pic1");
                    var nome2 = chatData[i].getAttribute("nom2"); //1
                    var pic2 = chatData[i].getAttribute("pic2"); //2
                    var text = chatData[i].getAttribute("text"); //3
                    var data = chatData[i].getAttribute("data"); //4 


                    var tutto = nome1 + ": " + text + "<br/>";
                    $('#content').append(tutto);
                    alert("appendo");
                }



                if (chatData.length > 0) {

                    $('#content').trigger();


                }

            });

        request.onreadystatechange = function () {
            if (request.readyState == 4) {

                aggiornaLastSeen("http://ritapp.altervista.org/chatData.php", function () {});

            }
        };

    }

    var request = window.ActiveXObject ?
        new ActiveXObject('Microsoft.XMLHTTP') :
        new XMLHttpRequest;

    function downloadUrl(url, callback) {

        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                callback(request, request.status);
            }
        };

        request.open('GET', url, true);
        request.send(null);

    }


    function aggiornaLastSeen(url, callback) {

        request.onreadystatechange = function () {
            if (request.readyState == 4) {
                callback(request, request.status);
            }
        };

        var sendUser = "type=" + "chatUser" + "&nome1=" + "CAVI" + "&pic1=" + "imm1";

        request.open('POST', url, true);
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.send(sendUser);

    }
4

0 回答 0