AJAX:在readyState=1时停止运行
AJAX: stops running at readyState = 1
这是我的脚本:
var Ajax = {
init: function(){
try{
this.xmlHttp = new XMLHttpRequest();
}
catch( e ){
try{
this.xmlHttp = new ActiveXObject( "Microsoft.XMLHttp" );
}
catch( e ){
}
}
return this;
},
get: function( url , async , fn ){
this.xmlHttp.open( "GET" , url , async );
console.log( "1" );
this.xmlHttp.setRequestHeader( 'Content-Type' , 'application/x-www-form-urlencoded' );
console.log( this.xmlHttp.readyState );
this.xmlHttp.onreadystatechange = function(){
console.log( "3" );
if( this.xmlHttp.readyState == 4 ){
if( this.xmlHttp.status == 200 ){
try{
if( fn ) return fn( this.xmlHttp.responseText );
}
catch( e ){
alert( e );
}
}
else alert( "bad status" );
}
else alert( "bad state" );
}
}
}
还有一条线路:
Ajax.init().get( "localhost/engine.php" , true , function( txt ){alert(txt)});
现在,在控制台上,我得到了这个:
1
1
因此,当readyState为1时,我理解this.xmlHttp.onreadystatechange = function(){
的运行停滞。你能告诉我这里怎么了吗?我是不是遗漏了一些更大的错误?
经过研究,xmlHttp似乎并没有真正打开url(在Chrome的控制台和Firebug中看不到请求)
您从未真正启动过请求!在get()函数的末尾添加以下内容:
this.xmlHttp.send();
希望这能帮助
相关文章:
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 检查在使用节点运行程序时是否给出ARGV
- "无法获取/null”;当使用nodejs运行服务器时
- 运行POST时显示图像
- 安卓系统:通过loadUrl运行javascript时,网络视图中的视频会冻结
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- ng中继器打印结果0;当我运行函数时
- 运行函数时出错
- 当我运行代码时,我得到以下结果 []对象对象] [对象对象],但应该给我一个有序数组
- AngularJS/Protactor:使用角种子基本应用程序运行量角器时出错
- 在ASP.Net中运行javascript时的执行顺序
- 如何在运行验证时保持ng模型值
- 在Microsoft Surface(Excel VBA)上运行GetJSObject时失败
- 硒 IDE 1.8.1 警报框:运行测试时看不到警报框
- 在Mac OSX上运行MongoDB时出现问题
- 运行 NodeJS 时出错
- jQuery 在运行 C# 方法时重置
- 当我通过 SHA1 运行字符串时的值与使用 .charCodeAt() 检查它时的值不同
- j查询在本地运行网站时未在 MAMP (1.11.2) 中加载
- 在 jQuery setInterval 中运行函数时出现问题