错误 'open' 调用了未实现接口 XMLHttpRequest 的对象
Error 'open' called on an object that does not implement interface XMLHttpRequest
当我尝试从另一个 class 函数在 class 中启动 ajax 请求时出现此错误。
你可以从这个例子中得到 firefox 控制台中的错误:
https://jsfiddle.net/d2o1d0eu/
http://codepen.io/anon/pen/dGwwKG
var MyClass = new Class({
initialize: function(options){
this.my_request = new Request({
url: '/echo/html/',
method: 'post',
onSuccess: function(resp) {
alert(resp);
}
});
},
request: function(param){
this.my_request.send('html='+param);
}
});
var MySecondClass = new Class({
Implements: [Options],
options: {
first_class: {}
},
initialize: function(options){
this.setOptions(options);
this.options.first_class.request('internal'); // error
},
request: function(param){
this.options.first_class.request(param);
}
});
var unknown_name = new MyClass();
// unknown_name.request('unknown_name test'); // ok
var test = new MySecondClass({
'first_class': unknown_name
});
// test.request('second class test'); // error
欢迎任何建议,谢谢。
在内部,Options
mixin 使用 Object.merge
,它对传递的 options
参数进行递归复制。
在您的代码中,您将 MyClass
的实例作为选项传递给 MySecondClass
的实例。当您以这种方式传递一个对象时,Object.merge
将递归地克隆该对象。这意味着在您的第二个 class 中,this.options.first_class.my_request
实际上是 Request
实例的 clone,实例的所有对象属性都被克隆为嗯。
这会带来问题,因为 Request
class 的实际 XMLHttpRequest
对象在该过程中被克隆。你最终得到的是一个基础对象(即它仅继承自 Object.prototype
),它具有实际 XMLHttpRequest
实例的所有属性和方法,但 不是 一个实际的例子。由于 XMLHttpRequest
的方法认为您正试图在非实例上调用这些方法,因此您会得到一个 DOMException。
要点是:不要将 Class
个实例作为选项传递 。相反,将它们作为单独的参数传递:new MySecondClass(unknown_name, options)
.
当我尝试从另一个 class 函数在 class 中启动 ajax 请求时出现此错误。 你可以从这个例子中得到 firefox 控制台中的错误:
https://jsfiddle.net/d2o1d0eu/
http://codepen.io/anon/pen/dGwwKG
var MyClass = new Class({
initialize: function(options){
this.my_request = new Request({
url: '/echo/html/',
method: 'post',
onSuccess: function(resp) {
alert(resp);
}
});
},
request: function(param){
this.my_request.send('html='+param);
}
});
var MySecondClass = new Class({
Implements: [Options],
options: {
first_class: {}
},
initialize: function(options){
this.setOptions(options);
this.options.first_class.request('internal'); // error
},
request: function(param){
this.options.first_class.request(param);
}
});
var unknown_name = new MyClass();
// unknown_name.request('unknown_name test'); // ok
var test = new MySecondClass({
'first_class': unknown_name
});
// test.request('second class test'); // error
欢迎任何建议,谢谢。
在内部,Options
mixin 使用 Object.merge
,它对传递的 options
参数进行递归复制。
在您的代码中,您将 MyClass
的实例作为选项传递给 MySecondClass
的实例。当您以这种方式传递一个对象时,Object.merge
将递归地克隆该对象。这意味着在您的第二个 class 中,this.options.first_class.my_request
实际上是 Request
实例的 clone,实例的所有对象属性都被克隆为嗯。
这会带来问题,因为 Request
class 的实际 XMLHttpRequest
对象在该过程中被克隆。你最终得到的是一个基础对象(即它仅继承自 Object.prototype
),它具有实际 XMLHttpRequest
实例的所有属性和方法,但 不是 一个实际的例子。由于 XMLHttpRequest
的方法认为您正试图在非实例上调用这些方法,因此您会得到一个 DOMException。
要点是:不要将 Class
个实例作为选项传递 。相反,将它们作为单独的参数传递:new MySecondClass(unknown_name, options)
.