日期:2014-05-16  浏览次数:20722 次

ajax 的hello Ajax 级别的问题:入门级,我却困扰了一天了,高手请指点.
我想实现下面的功能,点击“Ajax提交”按钮,在按钮下出现“hello Ajax!”。如下图所示:

这个程序用到了两个文件,这两个文件在同一个文件夹下:
第一个文件名称我起名为  178.html,代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript"  type="text/javascript">

function  Ajax(){
   // alert(  "进入ajax函数了");
    var  xmlHttpReq = null;
if (window.ActiveXObject)  {
    xmlHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest)  {
    xmlHttpReq=new XMLHttpRequest();
}

    if(xmlHttpReq != null){
   xmlHttpReq.open("GET","test.php",true); //调用open()方法并采用异步方式
   xmlHttpReq.onreadystatechange=RequestCallBack; //设置回调函数
   xmlHttpReq.send(null); //因为使用get方式提交 
   xmlHttpReq=null;
}

}



function  RequestCallBack(){
 
if(xmlHttpReq.readyState == 4){

if(xmlHttpReq.status == 200){

document.getElementById("resText").innerHTML =  xmlHttpReq.responseText;

 }    
    }   
}
</script> 
</head>




<body>

<input  type="button"  value="ajax提交"    onclick="Ajax()"  />

<div id="resText"></div>



</body>
</html>



在上面的文件中用到了test.php文件,此文件非常简单,只有一下三句话,代码如下:

<?php
   echo "Hello Ajax!";
?>

我用了好长时间运行,点击“ajax提交”按钮,下面就是不出现“Hello  Ajax!”的内容,我一句一句的和书上的例子对比,还是不行,只好求教给位高手,请不吝指教。谢谢。
  
ajax 入门

------解决方案--------------------
你访问的是php,你放到服务器运行了没有,直接拖进浏览器或者双击查看php不会解析php代码,代码也有问题


    var xmlHttpReq = null///////放到外面来,放里面就是局部变量,callback中无法引用
    function Ajax() {
        // alert(  "进入ajax函数了");
       /// var xmlHttpReq = null;
        if (window.ActiveXObject) {
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
     &nb