php readystate,php – 当readyState为4时,为什么我在脚本中调用的Ajax函数连续运行两次?...

所有,

我正在使用Head First Ajax书来学习Ajax.在第一章中,他们给出了一些我简化了一些代码示例.我添加了一堆警报,以了解发生了什么.这是代码:

HTML Ajax(index.php):

/p>

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Rob's Rock 'n' Roll Memorabilia

function createRequest() {

try {

request = new XMLHttpRequest();

} catch (tryMS) {

try {

request = new ActiveXObject("Msxml2.XMLHTTP");

} catch (otherMS) {

try {

request = new ActiveXObject("Microsoft.XMLHTTP");

} catch (failed) {

request = null;

}

}

}

return request;

}

function getDetails(img){

var title = img.title;

alert("getDetails1");

request = createRequest();

alert("getDetails2");

if (request == null) {

alert("Unable to create request");

return;

}

var url= "getDetails.php?ImageID=" + escape(title);

alert("getDetails3");

request.open("GET", url, true);

alert("getDetails4");

request.onreadystatechange = displayDetails;

alert("getDetails5");

request.send(null);

alert("getDetails6");

}

function displayDetails() {

alert("displayDetails1");

if (request.readyState == 4) {

alert("Request.readyState is 4");

if (request.status == 200) {

alert("Request.status is 200");

detailDiv = document.getElementById("description");

alert("displayDetails2");

detailDiv.innerHTML = request.responseText;

alert("displayDetails3");

}else{

alert("Request.status not 200");

return;

}

}else{

alert("Request.readystate not 4");

return;

}

}

SISL_Avatar2.JPG

title="SISL" id="SISL" οnclick="getNextImage()" />

guitar

title="itemGuitar" id="itemGuitar" οnclick="getDetails(this)"/>

sunglasses

title="itemShades" id="itemShades" οnclick="getDetails(this)" />

cowbell

title="itemCowbell" id="itemCowbell" οnclick="getDetails(this)" />

hat

title="itemHat" id="itemHat" οnclick="getDetails(this)" />

blank-detail.jpg

$details = array (

'itemGuitar' => "

Pete Townshend once played this guitar while his own axe was in the shop having bits of drumkit removed from it.

",

'itemShades' => "

Yoko Ono's sunglasses. While perhaps not valued much by Beatles fans, this pair is rumored to have been licked by John Lennon.

",

'itemCowbell' => "

Remember the famous \"more cowbell\" skit from Saturday Night Live? Well, this is the actual cowbell.

",

'itemHat' => "

Michael Jackson's hat, as worn in the \"Billie Jean\" video. Not really rock memorabilia, but it smells better than Slash's tophat.

"

);

if (isset($_REQUEST['ImageID'])){echo $details[$_REQUEST['ImageID']];}

?>

这是Ajax(getDetails.php)调用的URL:

$details = array (

'itemGuitar' => "

Pete Townshend once played this guitar while his own axe was in the shop having bits of drumkit removed from it.

",

'itemShades' => "

Yoko Ono's sunglasses. While perhaps not valued much by Beatles fans, this pair is rumored to have been licked by John Lennon.

",

'itemCowbell' => "

Remember the famous \"more cowbell\" skit from Saturday Night Live? Well, this is the actual cowbell.

",

'itemHat' => "

Michael Jackson's hat, as worn in the \"Billie Jean\" video. Not really rock memorabilia, but it smells better than Slash's tophat.

"

);

echo $details[$_REQUEST['ImageID']];

?>

问题:为什么函数displayDetails在readystate 4运行两次?

当我运行上面的代码时,代码似乎通过displayDetails()函数运行两次.我首先得到displayDetails1警报信号,我已进入该功能.然后我得到与readyState相关的警报不是4,然后再不是4,然后是4(Request.readyState是4).然后状态警报告诉我状态是200.到目前为止,没有任何意外.

在那之后我得到一些奇怪的东西.我得到displayDetails2警报,然后根据函数修改页面,我得到displayDetails3警报.然后我期望退出该功能.相反,我再次获得displayDetails1,Request.readyState是4(第二次!),Request.status是200,displayDetails2和displayDetails3警报,就像整个函数第二次运行一样.这是为什么?

PS:

1)在第二轮之后,我得到了我期望的getDetails6警报.

2)页面按原样运行 – 从用户的角度来看,如果禁用警报,则没有任何异常.

3)我在WampServer上本地开发WinXP机器(我知道……).

4)如果我添加:

function alert(msg) {

console.log(msg);

}

在我的脚本中,日志只注册一个readyState是4 …

解析度

我做了3次测试:

1 – 只有警报,我得到两个readyState是4个警报.

2 – 如果我记录警报,日志只显示一个readyState是4警报.

3 – 如果我同时记录并显示警报弹出窗口(使用this function),我会得到两个readyState是4个警报(日志显示).

我对此的看法是,警报本身会导致脚本执行延迟并导致函数有效运行两次.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值