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

标签 javascript scope xmlhttprequest closures event-handling

我在创建服务器推送网络对象时遇到问题。因为 Firefox 和 chrome 处理服务器推送的方式不同,(Firefox 获取 onload 事件,chrome 使用 onprogress/broken )。对于 chrome,我需要捕获 onprogress 事件,然后屏蔽掉响应文本中的先前数据。我只想将新数据传递给 chrome。

在 sendpush() 中,我使用两个函数来设置偶数处理程序。

我知道这与闭包和范围有关,

有什么IDE吗?

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);
 }
}

最佳答案

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

关于javascript - 在 XMLHttpRequest 事件处理程序中找不到字符串函数 (subStr),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3594121/

相关文章:

javascript - 按 Enter 键时的动态文本框

javascript - 这段 JavaScript 代码的正确语法是什么?

c - "File scope"和 "program scope"有什么区别

c - 如何使我的数组可用于其他函数?

javascript - 使用 xmlhttprequest 使用 alfresco RESTApi 创建一个人

javascript - 检查所有图表的数据并隐藏没有数据的图表

javascript - Webpack 在构建时生成文件

java - 变量可能尚未初始化,不确定如何解决

javascript - 使用D3 Queue进行跨站点调用

google-chrome - Chrome 是否支持 HTTP Vary header