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;
}
}
title="SISL" id="SISL" οnclick="getNextImage()" />
title="itemGuitar" id="itemGuitar" οnclick="getDetails(this)"/>
title="itemShades" id="itemShades" οnclick="getDetails(this)" />
title="itemCowbell" id="itemCowbell" οnclick="getDetails(this)" />
title="itemHat" id="itemHat" οnclick="getDetails(this)" />
$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个警报(日志显示).
我对此的看法是,警报本身会导致脚本执行延迟并导致函数有效运行两次.
php readystate,php – 当readyState为4时,为什么我在脚本中调用的Ajax函数连续运行两次?...相关推荐
- STM32+Cube MX使用MPU6050 DMP时,在外部中断中调用read_dmp函数发生系统卡死问题的根本原因分析
系统环境 硬件: MCU:STM32F405RGT6 IMU:MPU6050 软件: 开发环境:KEIL MDK-ARM uVision5 MPU6050使用DMP 复现步骤 程序中MPU6050 D ...
- 使用mysql-proxy读写分离时的注意事项_mysql-proxy中Admin Plugin的使用以及读写分离的问题...
一年前听Hiro说过MySQL Proxy这个应用,直到现在才来了兴致鼓弄鼓弄,也是因为来到这边后,有生产环境一直在用mysql-proxy实现应用的读写分离,前辈搭好的架构现在好好学习下.也就是从昨 ...
- C#中调用Windows API时的数据类型对应关系
C#中调用Windows API时的数据类型对应关系 原文 C#中调用Windows API时的数据类型对应关系 BOOL=System.Int32 BOOLEAN=System.Int32 BYTE ...
- python 实例方法调用时可以忽略第一个参数_盘点Python中易忽略的函数
Python 内置函数 1.ord() 函数是 chr() 函数(对于8位的ASCII字符串)或 unichr() 函数(对于Unicode对象)的配对函数,它以一个字符(长度为1的字符串)作为参数, ...
- 不使用三方包时,如何在ThinkSNS中建立优雅的用户权限管理
本文主要全面讲解在不适用第三方包的情况下,如何在基于Laravel框架上,研发社交系统ThinkSNS+时,简历一套优雅而不失性价比的用户权限管理体系功能,[内含ThinkSNS真实代码]. 需求场景 ...
- 编译时和运行时、OC中对象的动态编译机制
编译时 编译时顾名思义就是正在编译的时候.那啥叫编译呢?就是编译器帮你把源代码翻译成机器能识别的代码.(当然只是一般意义上这么说,实际上可能只是翻译成某个中间状态的语言.比如Java只有JVM识别的字 ...
- 当 IDENTITY_INSERT 设置为 OFF 时,不能为表中的标识列插入显式值
{"当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'OrderList' 中的标识列插入显式值"} 对于这个异常可以从两个角度来处理:A:数据库执行语句 B: ...
- 在没有任何数据时进行无效的读取尝试。_技术转载——JVM运行时内存是怎么分布的?...
写在前面:2020年面试必备的Java后端进阶面试题总结了一份复习指南在Github上,内容详细,图文并茂,有需要学习的朋友可以Star一下! GitHub地址:https://github.com/ ...
- 用友t3服务器文件丢失,用友T3软件在T3用友通标准版恢复账套时在备份的文件中找不到要恢复的文件,如何解决?-用友T3...
文章摘要:用友T3软件在T3用友通标准版恢复账套时在备份的文件中找不到要恢复的文件,如何解决?在T3用友通标准版恢复账套时在备份的文件中找不到要恢复的文件,如何解决? 问题模块: 系统管理关键字:备份 ...
- iOS运行时-使用Runtime向Category中添加属性以及运行时介绍
前言 了解OC的都应该知道,在一般情况下,我们是不能向Category中添加属性的,只能添加方法,但有些情况向,我们确实需要向Category中添加属性,而且很多系统的API也有一些在Category ...
最新文章
- 和12岁小同志搞创客开发:如何驱动 12864 OLED液晶显示屏?
- python apktool_Python使用ApkTool和子进程反编译APK
- Spring 数据源配置与应用
- 高效并发处理之libevent
- Linux中 set、env、declare、export显示shell变量的区别
- Java中Connection方法笔记
- protobuf 下载、安装、编译
- Ukiyo-e faces dataset 浮世绘面孔数据集
- 重要!VALSE 2021确认参会及第二次【预注册】事宜的通知
- visio画图中怎么添加①
- 【金融】【随机森林】使用随机森林对期货数据(涨跌)进行回归
- EUCM鱼眼相机模型详解
- 每天一点正则表达式积累之(?=X)和(?!X)测试(七)
- Android 通过usb调用高拍仪进行拍照
- 怎么找到使用驱动器中的光盘之前需要将其格式化磁盘的数据
- 完成基因序列(fasta格式)的提取,核苷酸计数,GC含量计算,DNA 翻译为 RNA,RNA 翻译为 蛋白质,反向序列获取的相关函数
- 如何锁定excel的一列不允许修改编辑
- Chrome 配置samesite=none方式
- SpringCloud 之分布式 CAP 定理
- 程序员宝宝们6661儿童节快乐
热门文章
- 全国青少年编程等级考试python二级真题2020年9月(含题库答题软件账号)
- Cannot lock file hash cache (E:\blackWu\github\X5WebView\WebViewX5\.gradle\4.6\fileHashes) as it has
- android录制amr音频文件,android – 以AMR文件格式录制音频
- 2020年4大安全会议及论文
- bzoj 4453 cys就是要拿英魂! —— 后缀数组+单调栈+set
- SURF(Speeded Up Robust Features)算法原理
- 编程十年 (14):毁人不倦2
- 摄影构图学83年绝版_学手机摄影最好要知道的81条忠告!都是大实话
- ChucK学习笔记(一)——下载与安装
- C语言函数题-输入多个单词,统计以指定字母开头的单词个数