无法使用纯JavaScript从AJAX对象检索结果
尝试实现自己的AJAX原型,而不是使用jQuery或其他原型。 我无法检索任何结果,断点设置在某行上,但未触发: 问题在于下一部分: 如果我以前使用init函数初始化了HttpXml实例,为什么它没有定义:无法使用纯JavaScript从AJAX对象检索结果,javascript,ajax,asynchronous,xmlhttprequest,prototype,Javascript,Ajax,Asynchronous,Xmlhttprequest,Prototype,尝试实现自己的AJAX原型,而不是使用jQuery或其他原型。 我无法检索任何结果,断点设置在某行上,但未触发: 问题在于下一部分: 如果我以前使用init函数初始化了HttpXml实例,为什么它没有定义: this.someEvent.onSomething = function( item ) { ... }; 我正在尝试从程序的其他部分发送请求,执行以下操作: var ajaxInstance = new GC3D.Ajax(); ajaxInstance.init(); var res
this.someEvent.onSomething = function( item ) { ... };
我正在尝试从程序的其他部分发送请求,执行以下操作:
var ajaxInstance = new GC3D.Ajax();
ajaxInstance.init();
var response = ajaxInstance.sendRequest({
HttpMethod: 'GET',
UrlEndpoint: '/SomeService?function=someFunctionName'
});
该原型的完整来源:
GC3D.Ajax = function() {
this.httpRequest = undefined;
this.listExceptions = undefined;
};
GC3D.Ajax.prototype.init = function() {
this.listExceptions = [];
if ( window.XMLHttpRequest ) this.httpRequest = new XMLHttpRequest();
else if ( window.ActiveXObject ) {
try {
this.httpRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
}
catch ( exception ) {
this.listExceptions.push( exception );
try {
this.httpRequest = new ActiveXObject( 'Msxml2.XMLHTTP' );
}
catch ( exception ) {
this.listExceptions.push( exception );
try {
this.httpRequest = new ActiveXObject( 'Microsoft.XMLHTTP' );
}
catch ( exception ) {
this.listExceptions.push( exception );
}
}
}
}
if ( !this.httpRequest ) {
console.error( 'Can\'t create a HTTP Request instance for AJAX! Possible problems:' );
console.error( this.listExceptions );
}
else this.httpRequest.onreadystatechange = this.getContentFromRequest;
};
GC3D.Ajax.prototype.sendRequest = function( properties ) {
if ( this.httpRequest !== undefined ) {
this.httpRequest.open( properties.HttpMethod, properties.UrlEndpoint );
this.httpRequest.send();
}
else throw 'HTTP Request instance isn\'t defined!';
};
GC3D.Ajax.prototype.getContentFromRequest = function() {
if ( this.httpRequest !== undefined ) {
if ( this.httpRequest.readyState === 4) {
if ( this.httpRequest.status === 200 ) return this.httpRequest.responseText;
else console.log( 'There was a problem with the request in GC3D.Ajax.' );
}
}
};
GC3D.Ajax.prototype.get = function() {
return this.httpRequest;
};
什么是不正确的,为什么它没有向上面的那条线开火
谢谢问题在于此上下文在此处丢失:
else this.httpRequest.onreadystatechange = this.getContentFromRequest;
当我申请那个事件时,上面的函数已经丢失了这个实例
让我们想象一下,我们不知道确切的函数名,它将是一个匿名函数:
this.someEvent.onSomething = function( item ) { ... };
在这里,它在匿名函数中丢失了,因为作用域定义{},它关闭了这个函数的可见空间
因此,当我将某些部分更改为:
GC3D.Ajax.prototype.getContentFromRequest = function() {
if ( this.readyState === 4 ) {
if ( this.status === 200 ) return this.responseText;
else console.log( 'There was a problem with the request in GC3D.Ajax.' );
}
};
现在代码开始工作了 您从未调用getContentFromRequest。@dfsq从未显式调用,但在此处的事件上具有bind as函数调用:else this.httpRequest.onreadystatechange=this.getContentFromRequest@dfsq还。。。似乎您没有注意到,如果您仔细查看Q内容中的第二个屏幕,您可能会在getContentFromRequest中看到确切的断点,并且它被命中,但是没有定义xmlHttp实例。有关此的更多详细信息:在this变量下