初学ajax,写了个小程序,碰到问题,请大师们指点迷津啊,TKS

wangjingyong0305 2011-07-02 10:01:03
调试提示
xmlHttp.send(null) ; 这有问题 请帮忙看看
ajax_receive_content.html:
<hmtl>
<head>
<title>Ajax实例</title>
<script language="javascript">
var xmlHttp ; //AJAX核心对象的名称
function createXMLHttp(){ //创建XMLHttpRequest核心对象
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest() ;
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function showMsg(){
createXMLHttp() ; //创建XMLHttp对象
xmlHttp.open("post","content.html") ;
xmlHttp.onreadystatechange = showMsgCallback() ;
xmlHttp.send(null) ;
}
function showMsgCallback(){ //回调操作函数
if(xmlHttp.readyState == 4){ //调用完成
if(xmlHttp.status == 200){ //HTTP状态码操作正常
var text = xmlHttp.responseText ;
document.getElementById("msg").innerHTML = text ; //将内容显示出
}
}
}
</script>
</head>
<body>
<input type="button" onClick="showMsg()" value="调用ajax显示内容">
<span id="msg"></span>
</body>
</html>

create_ajax.html:
<hmtl>
<head>
<title>Ajax实例</title>
<script language="javascript">
var xmlHttp ; //AJAX核心对象的名称
function createXMLHttp(){ //创建XMLHttpRequest核心对象
if(window.XMLHttpRequest){
xmlHttp = new XMLHttpRequest() ;
}else{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
</script>
</head>
<body>
</body>
</html>

content.html:
hello word!
...全文
125 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuelang1225 2011-07-04
  • 打赏
  • 举报
回复
帮你顶起去。
weike021996 2011-07-03
  • 打赏
  • 举报
回复
post 换成get 而且把xmlHttp.onreadystatechange = showMsgCallback() ; 括号去掉
KeepMoving 2011-07-03
  • 打赏
  • 举报
回复
2楼4楼都是正解!
gkmzk 2011-07-03
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax实例</title>
<script language="javascript">

function showMsg() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("msg").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "content.html", true);
xmlhttp.send();
}
</script>
</head>


<body>
<input type="button" onClick="showMsg()" value="调用ajax显示内容">
<span id="msg"></span>
</body>
</html>
weike021996 2011-07-03
  • 打赏
  • 举报
回复
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax实例</title>
<script language="javascript">

function showMsg() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("msg").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "content.html", true);
xmlhttp.send();
}
</script>
</head>


<body>
<input type="button" onClick="showMsg()" value="调用ajax显示内容">
<span id="msg"></span>
</body>
</html>
彭金华 2011-07-03
  • 打赏
  • 举报
回复
建议初学可以先学会第三方ajax库,不然费太多时间,会了之后再从第三方库开始研究!
wuxia2118 2011-07-02
  • 打赏
  • 举报
回复

据说用POST方式要在send方法之前设置下面这句
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

不过我加了也不行,换成get应该行吧
caibird1024 2011-07-02
  • 打赏
  • 举报
回复
ls的已经帮你找到问题了 呵呵
heylnsld 2011-07-02
  • 打赏
  • 举报
回复
xmlHttp.onreadystatechange = showMsgCallback() ;

这一句,把最后的括号去掉

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧