在 XMLHttpRequest 事件处理程序中找不到字符串函数 (subStr)
我在创建服务器推送网络对象时遇到问题。因为 Firefox 和 chrome 处理服务器推送的方式不同,(Firefox 获取 onload 事件,chrome 使用 onprogress /broken )。对于 chrome,我需要捕获 onprogress 事件,然后屏蔽掉响应文本中的先前数据。我只想将新数据传递给 chrome。
在 sendpush() 中,我使用两个函数来设置偶数处理程序。
我知道这与闭包和范围有关,有
什么想法吗?
function prog( evt, obj, func ) {
var r = evt.target;
var t = typeof( r.responseText);
var z = "test".subStr( 2 );
var d = r.responseText.subString( obj.oldLen );
var s = {};
s["code"] = r.status;
s["text"] = r.statusText;
func( s, d );
};
function maRequest( url, method, multi ) {
this.method = "POST";
this.multi = false;
this.url = url;
this.self = this;
this.oldLen = 0;
if ( method != undefined )
this.method = method;
if ( multi != undefined )
this.multi = multi;
var req = new XMLHttpRequest();
this._req = req;
req.multipart = this.multi;
/* this._req.onreadystatechange = function( evt ) {*/
this.send = function( data, loadcb ) {
this._req.onload = function( evt ) {
var r = evt.target;
var stat = {};
stat["code"] = r.status;
stat["text"] = r.statusText;
loadcb( stat, r.responseText );
return false;
};
this._req.open( this.method, this.url, true );
try {
this._req.send( data );
} catch (e) {
alert( e );
}
};
this.setProgress = function( func ) {
var self = this;
self._req.onprogress = function ( evt ) {
return prog( evt, self, func );
}
};
this.addCallback = function( name, func ) {
var self = this;
var cb = function ( evt ) {
func( evt, self );
}
switch( name ) {
case "loadstart":
req.onloadstart = cb;
break;
case "progress":
req.onprogress = cb;
break;
case "abort":
req.onabort = cb;
break;
case "error":
req.onerror = cb;
break;
default:
req.addEventListener( name, function ( evt ) {
func( evt, self );
}, false);
};
};
this.abort = function() {
req.abort();
};
};
// works when passed as callback function
function progress( evt, obj) {
var req = evt.target;
stat = req.status;
alert( req.statusText+" "+req.readyState+" "+ req.responseText.substr( obj.oldLen) );
obj.oldLen = req.responseText.length;
return true;
}
function maChat() {
this.url = "/mafw/chat.ma";
}
maChat.prototype = new maRequest( this.url );
function callback( status, data ) {
alert( status["code"]+status["text"]+" "+data );
}
function sendmsg( ) {
var cmd = {};
cmd["type"] = "cmd";
cmd["cmd"] = "initdata";
cmd["me"] = 0;
// try {
// var conn = new maRequest( "http://localhost/mafw/chat.ma" );
// conn.send( JSON.stringify ( cmd ), callback );
// } catch (e ) {
// alert (e);
// }
try {
var conn1 = new maChat();
conn1.send( JSON.stringify ( cmd ), callback );
} catch (e ) {
alert (e);
}
};
function sendpush() {
var cmd = {};
cmd["type"] = "cmd";
cmd["cmd"] = "initdata";
cmd["me"] = 0;
try {
var conn = new maRequest( "http://localhost/mafw/chat.psh", "POST", true );
//conn.addCallback( "progress", progress );
conn.setProgress( callback );
conn.send( JSON.stringify ( cmd ), callback );
} catch (e ) {
alert (e);
}
}
I am have a problem with creating a server push network object. Because Firefox and chrome handle server push differently, (Firefox get onload events, chrome uses onprogress /broken ). For chrome I need to capture the onprogress events and then mask off the previous data from the responseText. I want to pass just the new data to chrome.
in sendpush() there are two functions the I use to set the even handler.
I know this has something to do with closure and scope,
Any ides?
function prog( evt, obj, func ) {
var r = evt.target;
var t = typeof( r.responseText);
var z = "test".subStr( 2 );
var d = r.responseText.subString( obj.oldLen );
var s = {};
s["code"] = r.status;
s["text"] = r.statusText;
func( s, d );
};
function maRequest( url, method, multi ) {
this.method = "POST";
this.multi = false;
this.url = url;
this.self = this;
this.oldLen = 0;
if ( method != undefined )
this.method = method;
if ( multi != undefined )
this.multi = multi;
var req = new XMLHttpRequest();
this._req = req;
req.multipart = this.multi;
/* this._req.onreadystatechange = function( evt ) {*/
this.send = function( data, loadcb ) {
this._req.onload = function( evt ) {
var r = evt.target;
var stat = {};
stat["code"] = r.status;
stat["text"] = r.statusText;
loadcb( stat, r.responseText );
return false;
};
this._req.open( this.method, this.url, true );
try {
this._req.send( data );
} catch (e) {
alert( e );
}
};
this.setProgress = function( func ) {
var self = this;
self._req.onprogress = function ( evt ) {
return prog( evt, self, func );
}
};
this.addCallback = function( name, func ) {
var self = this;
var cb = function ( evt ) {
func( evt, self );
}
switch( name ) {
case "loadstart":
req.onloadstart = cb;
break;
case "progress":
req.onprogress = cb;
break;
case "abort":
req.onabort = cb;
break;
case "error":
req.onerror = cb;
break;
default:
req.addEventListener( name, function ( evt ) {
func( evt, self );
}, false);
};
};
this.abort = function() {
req.abort();
};
};
// works when passed as callback function
function progress( evt, obj) {
var req = evt.target;
stat = req.status;
alert( req.statusText+" "+req.readyState+" "+ req.responseText.substr( obj.oldLen) );
obj.oldLen = req.responseText.length;
return true;
}
function maChat() {
this.url = "/mafw/chat.ma";
}
maChat.prototype = new maRequest( this.url );
function callback( status, data ) {
alert( status["code"]+status["text"]+" "+data );
}
function sendmsg( ) {
var cmd = {};
cmd["type"] = "cmd";
cmd["cmd"] = "initdata";
cmd["me"] = 0;
// try {
// var conn = new maRequest( "http://localhost/mafw/chat.ma" );
// conn.send( JSON.stringify ( cmd ), callback );
// } catch (e ) {
// alert (e);
// }
try {
var conn1 = new maChat();
conn1.send( JSON.stringify ( cmd ), callback );
} catch (e ) {
alert (e);
}
};
function sendpush() {
var cmd = {};
cmd["type"] = "cmd";
cmd["cmd"] = "initdata";
cmd["me"] = 0;
try {
var conn = new maRequest( "http://localhost/mafw/chat.psh", "POST", true );
//conn.addCallback( "progress", progress );
conn.setProgress( callback );
conn.send( JSON.stringify ( cmd ), callback );
} catch (e ) {
alert (e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是小写的,使用
substr
,而不是subStr
。substring
也是如此,它也应该是小写。It's in lowercase, use
substr
, notsubStr
. The same goes forsubstring
, it should be lowercase too.