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

article/2025/11/4 23:48:21

所有,

我正在使用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个警报(日志显示).

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


http://chatgpt.dhexx.cn/article/XmNyYRt8.shtml

相关文章

readyState与status的几种状态

什么是readyState readyState是XMLHttpRequest对象的一个属性,用来标识当前XMLHttpRequest对象处于什么状态。 readyState总共有5个状态值,分别为0~4,每个值代表了不同的含义,如下表所示:    0 未初始化状态&#…

等宽编程字体Consoles YaHei

英文字体是Consolas, 中文字体是微软雅黑。 已经进行了修改与合并,1个中文宽度2个英文宽度 中英文完全等宽,适合编程使用。 注意:xp系统没有ClearType功能,使用该字体效果不加,可以使用第三方字体渲染软…

几种码农使用的等宽字体比较——让自己的眼睛爽起来!

以前在编程时也注意过字体的选择,甚至有一段时间也一直坚持在记事本中用使用fixedsys字体,虽然它只能显示12磅字号;也偏爱在Onenotes中使用Tahoma字体,因为显示的数字好看一些;PLSQLdeveloper中都统一为宋体11磅&#…

Ubuntu下VSCode使用等宽字体设置

1、VSCode默认字体不等宽,缩进也较小 2、文件---首选项---设置---常用设置 3、将上面的Font Family设置改成如下 4、字体变成等宽的,缩进也改变了,Font Size可改变字体大小

字体大小,行高,高度

ie6下,font-size 等于 line-height 时,字体受行高限制,显示不全。 如图: 所以应该将行高设置为大于字体值2px,切记不要这样写font:12px/12px,要12px/14px /// 1,font-size 没有设置…

与中文对齐的英文等宽字体

与中文对齐的英文等宽字体 原始文档:https://www.yuque.com/lart/blog/wbhfef 前言 等宽字体是编程中常会用到的一些字体, 基本的形式就是所有符号的宽度都是一样的. 但由于大多数等宽字体设计时并未考虑对中文字符的兼容, 所以我们在使用时会面对这样一个问题, …

两个优美的等宽字体(支持中文等)

字体来源:http://bbs.themex.net/showthread.php?t16894635 这位大神除了中文还集成了其他很多东西 (以下字体均开了抗锯齿特效,实际效果略有差别) XHei NSimSun.Yuan XHei NSimSun.ShinYaLan 虽然说VS中中文是不会太丑的&#x…

html显示等宽字体

使用code标签 <code> aaa </code> <br> <code> iii </code> 效果&#xff1a;

字体:等宽字体与比例字体 - Monospaced font Proportional font

区别 比例字体&#xff08;Monospaced Font&#xff09;即每个字母宽度按一定比例自动调整&#xff1b;而等宽字体则是每个字母所占宽度相同。 差别关键在于可读性。显然比例字体使单词的整体可读性增强了。但是&#xff0c;早期的打字机、电脑屏幕由于技术的限制&#xff0c;…

Android等宽字体

Android等宽字体 效果图 在xml中设置 添加属性 android:typeface"monospace" 例如 <TextViewandroid:layout_width"wrap_content"android:layout_height"wrap_content"android:layout_marginTop"10dp"android:text"123456789…

html设置等宽字体效果

<!DOCTYPE html><html><head><meta charset"utf-8"/><title>设置等宽文字 </title></head><body>下面将显示两端相同的英文效果&#xff0c;突出等宽文字与普通文字的效果<br/><!--下面这段英文使用了正常…

CSS font-family 等宽字体

CSS font-family 等宽字体 font-family: "Lucida Console", Consolas, "Courier New", Courier, monospace; font-family: Courier New, Courier, Lucida Console, Consolas, monospace; font-family: Courier, Lucida Console, Consolas, Courier New,…

等宽字体和比例字体

开头 前段时间&#xff0c;我负责维护的某内部平台&#xff0c;运营人员发了一条2020年以来的首篇公告&#xff0c;然后便出现了下图的案发现场&#xff08;其实我一直都没发现_&#xff0c;还是可爱的大佬提醒了一下&#xff09;&#xff0c;事出反常必有妖&#xff01; 吭吭…

等宽字体与非等宽字体_我最喜欢的等宽字体

等宽字体与非等宽字体 If you’ve read a few of my other pieces, you already know that I’m kinda backwards. I enjoy old tech (where applicable) and the nostalgia and focus associated with using it. So, it comes of no surprise, that I have a thing for old ty…

等宽字体 Monospaced Font

等宽字体&#xff08;monospaced font&#xff09;是指字符宽度相同的电脑字体。东亚语言中&#xff0c;方块字基本上都作为等宽字体处理&#xff0c;如各个地区的汉字、日语假名的全形字符、韩语谚文音节等字符都是等宽的。在传统西文印刷中&#xff0c;比例字体可以提高单词的…

树莓派入门之—树莓派3B+不支持ExFAT格式而启动不了的解决方案

软件硬件 硬件 》 mac pro 树莓气派3B软件 》NOOBS_v3_2_0.zip SDCardFormatterv5_Mac.zip流程 》按官网的指引 raspberry-pi-setting-up 来安装系统与启动 问题现像 插入SD卡启动电源后&#xff0c;树莓派的状态LED仅有电源灯亮&#xff08;红色的&#xff09;&#xff0…

树莓派3B+插入电源后状态灯只有红灯常亮,无法接入屏幕

这种情况一般是树莓派系统并没有正常启动&#xff0c;特别是对于首次安装树莓派系统的新人来说&#xff0c;会一直以为自己装的系统的原因&#xff0c;一直尝试重新安装系统&#xff0c;首先给大家说一下确定问题的方法&#xff1a; 1、确认内存卡插入正确&#xff1b;树莓派没…

树莓派4B爽上流安装python3的OpenCV(人脸检测识别—门禁“环境搭建篇”)

树莓派4B安装OpenCV3.4.0人脸检测识别—门禁“环境搭建篇” 前言一、准备工作PC端软件 二、搭建树莓派环境拓展TF内存卡Tips&#xff1a;换源&#xff08;python软件源&#xff09; 修改树莓默认Python版本扩展交互空间 “进击”的工具包们OpenCV编译工具OpenCV图像包OpenCV视频…

【方法】树莓派开机使用教程(看这个就够了)

文章目录 Ⅰ、安装树莓派系统Ⅱ、连接树莓派一、接外设直接当作电脑来用二、远程连接1、有网线1)ssh远程连接2)使用xrdp远程桌面3)使用VNC远程连接树莓派桌面 2、无网线 Ⅲ、一些常用指令 Ⅰ、安装树莓派系统 【更新】现在官方提供了直接的镜像写入方案&#xff0c;就可以不需…

树莓派开箱+上手python小游戏

树莓派开箱&上手python小游戏 树莓派开箱&上手python小游戏安装所需设备烧录系统使用NOOBS方式烧录失败下载官方系统镜像文件烧录成功 配置WiFi和SSH一、WiFi配置二、使用PuTTY登录到树莓派 上手Python小游戏乌龟赛跑小游戏 树莓派开箱&上手python小游戏 记录第一…