我正在与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);
}