利用原型设置AJAX时返回值的奇怪表现 本帖最后由 generalxu 于 2010-06-10 15:24:20 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可能是ajax异步请求的原因,还没等返回呢就接着执行下面的代码了 s.ajaxrequest(); alert(s.response)你用下状态判断,你ff看下它执行了几次 js代码是单线程执行,你发送ajax后,有可能还没等到tmp.status==200呢,就执行了alert(s.response).应该绑定个回调事件。<script type='text/javascript'> var ajax=function (){}; ajax.prototype.xmlhttp=null; ajax.prototype.url=''; //ajax.prototype.response=null; ajax.prototype.seturl=function(url) { this.url=url; } ajax.prototype.setaction=function(action) { this.action=action; } ajax.prototype.setstr=function(str) { this.str=str; } ajax.prototype.callBack = { success : function(o){} ,fail : function(o){} } ajax.prototype.ajaxrequest=function() { if(window.ActiveXObject) { this.xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); } else if(window.XMLHttpRequest) { this.xmlhttp=new XMLHttpRequest(); } if(this.xmlhttp!=null) { var tmp = this.xmlhttp; var self = this; this.xmlhttp.onreadystatechange=function () { if(tmp.readyState==4) { if(tmp.status==200) { // ajax.prototype.response=tmp.responseText; self.callBack.success.apply(tmp,[tmp.responseText]) } else { self.callBack.success.apply(tmp,[tmp.statusText]) } } } if(this.action=='get') { this.xmlhttp.open('GET',this.url,true); this.xmlhttp.send(null); } else if(this.action=='post') { this.xmlhttp.open('POST',this.url,true); this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); this.xmlhttp.send(this.str); } } else { alert('Your browser does not support AJAX'); } } function click1() { alert(1); var s= new ajax; s.seturl('s1.php?'+Math.random()); // s.setaction('alert'); s.setstr(null); s.setaction('get'); s.ajaxrequest(); s.callBack = { success : function(text){ alert(text); } ,fail : function(text){ alert("Error:"+text); } } s=null; } </script> <script type='text/javascript'> var ajax=function (){}; ajax.prototype.xmlhttp=null; ajax.prototype.url=''; //ajax.prototype.response=null; ajax.prototype.seturl=function(url) { this.url=url; } ajax.prototype.setaction=function(action) { this.action=action; } ajax.prototype.setstr=function(str) { this.str=str; } ajax.prototype.callBack = { success : function(o){} ,fail : function(o){} } ajax.prototype.ajaxrequest=function() { if(window.ActiveXObject) { this.xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); } else if(window.XMLHttpRequest) { this.xmlhttp=new XMLHttpRequest(); } if(this.xmlhttp!=null) { var tmp = this.xmlhttp; var self = this; this.xmlhttp.onreadystatechange=function () { if(tmp.readyState==4) { if(tmp.status==200) { // ajax.prototype.response=tmp.responseText; self.callBack.success.apply(tmp,[tmp.responseText]) } else { self.callBack.fail.apply(tmp,[tmp.statusText]) } } } if(this.action=='get') { this.xmlhttp.open('GET',this.url,true); this.xmlhttp.send(null); } else if(this.action=='post') { this.xmlhttp.open('POST',this.url,true); this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); this.xmlhttp.send(this.str); } } else { alert('Your browser does not support AJAX'); } } function click1() { var s= new ajax; s.seturl('s1.php?'+Math.random()); // s.setaction('alert'); s.setstr(null); s.setaction('get'); s.ajaxrequest(); s.callBack = { success : function(text){ alert(text); } ,fail : function(text){ alert("Error:"+text); } } s=null; } </script> php如何解析jquery serialize 提交后的数据 php的ftok函数在windows下能用吗? 网页怎样跳转 我是初学,请高手指教 很久不上来了 散分散分 PHP怎么判断字符串中包含重复相同的数字? 写出你的经典代码?给代码有分? 关于unset($GLOBALS) 关于php的mysql注入漏洞,有兴趣的进来看一下~~~ session怪问题(急.....) 数组归类问题 往类中添加数组如何实现?
alert(s.response)
你用下状态判断,你ff看下它执行了几次
应该绑定个回调事件。<script type='text/javascript'> var ajax=function (){};
ajax.prototype.xmlhttp=null;
ajax.prototype.url='';
//ajax.prototype.response=null;
ajax.prototype.seturl=function(url)
{
this.url=url;
}
ajax.prototype.setaction=function(action)
{
this.action=action;
}
ajax.prototype.setstr=function(str)
{
this.str=str;
}
ajax.prototype.callBack = {
success : function(o){}
,fail : function(o){}
}
ajax.prototype.ajaxrequest=function()
{
if(window.ActiveXObject)
{
this.xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
else if(window.XMLHttpRequest)
{
this.xmlhttp=new XMLHttpRequest();
}
if(this.xmlhttp!=null)
{
var tmp = this.xmlhttp;
var self = this;
this.xmlhttp.onreadystatechange=function ()
{
if(tmp.readyState==4)
{
if(tmp.status==200)
{
// ajax.prototype.response=tmp.responseText;
self.callBack.success.apply(tmp,[tmp.responseText])
}
else
{
self.callBack.success.apply(tmp,[tmp.statusText])
}
}
}
if(this.action=='get')
{
this.xmlhttp.open('GET',this.url,true);
this.xmlhttp.send(null);
}
else if(this.action=='post')
{
this.xmlhttp.open('POST',this.url,true);
this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
this.xmlhttp.send(this.str);
}
}
else
{
alert('Your browser does not support AJAX');
}
}
function click1()
{
alert(1);
var s= new ajax;
s.seturl('s1.php?'+Math.random());
// s.setaction('alert');
s.setstr(null);
s.setaction('get');
s.ajaxrequest();
s.callBack = {
success : function(text){
alert(text);
}
,fail : function(text){
alert("Error:"+text);
}
}
s=null;
}
</script>
ajax.prototype.xmlhttp=null;
ajax.prototype.url='';
//ajax.prototype.response=null;
ajax.prototype.seturl=function(url)
{
this.url=url;
}
ajax.prototype.setaction=function(action)
{
this.action=action;
}
ajax.prototype.setstr=function(str)
{
this.str=str;
}
ajax.prototype.callBack = {
success : function(o){}
,fail : function(o){}
}
ajax.prototype.ajaxrequest=function()
{
if(window.ActiveXObject)
{
this.xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
else if(window.XMLHttpRequest)
{
this.xmlhttp=new XMLHttpRequest();
}
if(this.xmlhttp!=null)
{
var tmp = this.xmlhttp;
var self = this;
this.xmlhttp.onreadystatechange=function ()
{
if(tmp.readyState==4)
{
if(tmp.status==200)
{
// ajax.prototype.response=tmp.responseText;
self.callBack.success.apply(tmp,[tmp.responseText])
}
else
{
self.callBack.fail.apply(tmp,[tmp.statusText])
}
}
}
if(this.action=='get')
{
this.xmlhttp.open('GET',this.url,true);
this.xmlhttp.send(null);
}
else if(this.action=='post')
{
this.xmlhttp.open('POST',this.url,true);
this.xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
this.xmlhttp.send(this.str);
}
}
else
{
alert('Your browser does not support AJAX');
}
}
function click1()
{
var s= new ajax;
s.seturl('s1.php?'+Math.random());
// s.setaction('alert');
s.setstr(null);
s.setaction('get');
s.ajaxrequest();
s.callBack = {
success : function(text){
alert(text);
}
,fail : function(text){
alert("Error:"+text);
}
}
s=null;
}
</script>