做一个留言程序时没反应,测试了下是因为readystate为undefined。后台也可以讲一下,网上搜来是众说纷纭
源码如下:
chat.jsfunction sendmsg(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
alert("\\你的浏览器不支持Ajax!");
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readystate == 4){
if(xmlHttp.status == 200){
document.getElementById("123").innerHTML = xmlHttp.responseText;
}
}else{
alert("错误! 进展:"+xmlHttp.status+"; 状态:"+xmlHttp.readystate);
}
}
var msg = document.getElementById("mytext").innerHTML;
var enmsg = encodeURIComponent(msg);
xmlHttp.open("POST","server.php?",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencode");
xmlHttp.send("enmsg="+ enmsg);
}后台的 server.php<?php
//header("Cache-Control:no-cache;must-revalidate");
$enmsg = file_get_contents("php://input");
echo "<li>".$enmsg["enmsg"]."</li>";
?>
请求好人帮帮忙
源码如下:
chat.jsfunction sendmsg(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
alert("\\你的浏览器不支持Ajax!");
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readystate == 4){
if(xmlHttp.status == 200){
document.getElementById("123").innerHTML = xmlHttp.responseText;
}
}else{
alert("错误! 进展:"+xmlHttp.status+"; 状态:"+xmlHttp.readystate);
}
}
var msg = document.getElementById("mytext").innerHTML;
var enmsg = encodeURIComponent(msg);
xmlHttp.open("POST","server.php?",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencode");
xmlHttp.send("enmsg="+ enmsg);
}后台的 server.php<?php
//header("Cache-Control:no-cache;must-revalidate");
$enmsg = file_get_contents("php://input");
echo "<li>".$enmsg["enmsg"]."</li>";
?>
请求好人帮帮忙
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
document.getElementById("123").innerHTML = xmlHttp.responseText;
}else{
alert("错误! 进展:"+xmlHttp.status+"; 状态:"+xmlHttp.readyState);
} }
第二个单词state S要大写
没办法死规定
$.get('text.php',{id:'1'},function(data){
//随便你要执行的js 代码 你说jquery难不难???
});//就这样ajax
function sendmsg(){
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest();
}
else if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else{
alert("\\你的浏览器不支持Ajax!");
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4){//重点在这readyState 的S要大写
if(xmlHttp.status == 200){
document.getElementById("123").innerHTML = xmlHttp.responseText;
}
}else{
//这个地方已经取不到xmlHttp.status的值了
//alert("错误! 进展:"+xmlHttp.status+"; 状态:"+xmlHttp.readystate);
}
}
var msg = document.getElementById("mytext").innerHTML;
var enmsg = encodeURIComponent(msg);
xmlHttp.open("POST","server.php?",true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencode");
xmlHttp.send("enmsg="+ enmsg);
}$enmsg = file_get_contents("php://input");
parse_str($enmsg);
echo "<li>".$enmsg."</li>";
jquery我还没研究过,还处于学习阶段,还是不必了,因为给我我暂时看不懂
接着就会研究
很感谢
另外我也犯了个低级错误——var msg = document.getElementById("mytext").innerHTML;是一个textarea取文本要用value而不是innerHTML;
谢谢你的纠正
再次代表党和国家感谢Ladies and乡亲们!!