问题:
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文件发请求?
求解释
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文件发请求?
求解释
看看headers里 query string paramenters的情况,response headers的情况,再分析。