问题:
ajax对象:AjaxRequest.jsvar net=new Object();
net.AjaxRequest=function(url,onload,onerror,method,params){
  this.req=null;
  this.onload=onload;
  this.onerror=(onerror) ? onerror : this.defaultError;
  this.loadDate(url,method,params);
}net.AjaxRequest.prototype.loadDate=function(url,method,params){
  if (!method){
    method="GET";
  }
  if (window.XMLHttpRequest){
    this.req=new XMLHttpRequest();
  } else if (window.ActiveXObject){
    this.req=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (this.req){
    try{
      var loader=this;
      this.req.onreadystatechange=function(){
        net.AjaxRequest.onReadyState.call(loader);
      }      this.req.open(method,url,true);
  if(method=="POST"){
this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  }
      this.req.send(params);
    }catch (err){
      this.onerror.call(this);
    }
  }
}
net.AjaxRequest.onReadyState=function(){
  var req=this.req;
  var ready=req.readyState;
  if (ready==4){
    if (req.status==200 ){
      this.onload.call(this);
    }else{
      this.onerror.call(this);
    }
  }
}net.AjaxRequest.prototype.defaultError=function(){
  alert("error fetching data!"
    +"\n\nreadyState:"+this.req.readyState
    +"\nstatus: "+this.req.status
    +"\nheaders: "+this.req.getAllResponseHeaders());
}
ajax对象的调用:index.jsfunction holdout(id,hits){
if(id>0){
alert("ok");
var loader=new net.AjaxRequest("hit.php?id="+id+"&hits="+hits+"&nocache="+new Date().getTime(),deal,onerror,"GET");
}
}
function onerror(){
alert("出错了,请重新打开!");
window.location.href="index.php";
}
function deal(){
fq_id=this.req.responseText;
var hitsId="fq_id"+fq_id.substr(0,fq_id.indexOf("#"));
hitsId=hitsId.replace(/\s/g,""); //去除字符串中的Unicode空白符
var hitsNum=fq_id.substr(fq_id.indexOf("#")+1,fq_id.length-fq_id.indexOf("#")-1);
//alert(hitsId);
document.getElementById(hitsId).innerHTML=hitsNum;
}
发送请求页面:index.php<script language="javascript" src="AjaxRequest.js"></script>  
<script language="javascript" src="index.js"></script> 
<a href='#' onclick='holdout(123,456)'>[测试]</a>  
<span id='fq_id123'>0</span>  php处理页面:hit.php
<?php
require("global.php");
require("function.php");
$id =$_GET['id'];
$hits = $_GET['hits'];
//echo $id."--".$hits;
$ins="update tb_wishes set hits=hits+1 where id=$id";
file_put_contents("d:/ajax_log2.txt","ok"."\r\n",FILE_APPEND);
$DB->query($ins);
$sql="select * from tb_wishes where id=$id";
$info=$DB->fetch_one_array($sql);
echo $hit=$id."#".$info['hits'];
?>
请教:
d:/ajax_log2.txt文件中没有数据,难道没有向hit.php文件发请求?  
求解释

解决方案 »

  1.   

    没有时间把你的代码运行一遍,而且出错的可能有各种。建议你直接访问 hit.php?id=123&hits=456 看看是否有 d:/ajax_log2.txt 存在。确认问题出在浏览器端还是服务器端脚本上。再去逐步缩小范围
      

  2.   

    google chrome,右键-》审查元素-》network 先打开,然后刷新index.php页面,点击<a href='#' onclick='holdout(123,456)'>[测试]</a> , 查看network左侧 找到hit.php,鼠标点击,查看右侧的headers, preview。
    看看headers里 query string paramenters的情况,response headers的情况,再分析。
      

  3.   

    使用JQuery库,这个问题就简单了