在 XMLHttpRequest 事件处理程序中找不到字符串函数 (subStr)

发布于 2024-09-16 06:53:27 字数 3109 浏览 3 评论 0原文

我在创建服务器推送网络对象时遇到问题。因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合久必婚 2024-09-23 06:53:27

它是小写的,使用 substr,而不是 subStrsubstring 也是如此,它也应该是小写。

It's in lowercase, use substr, not subStr. The same goes for substring, it should be lowercase too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文