Ajax未在Javascript库中获取响应文本
我正在编写一个Javascript库,其中包含以下代码: 构造函数(创建初始密钥并创建XMLHTTP请求对象):Ajax未在Javascript库中获取响应文本,javascript,ajax,Javascript,Ajax,我正在编写一个Javascript库,其中包含以下代码: 构造函数(创建初始密钥并创建XMLHTTP请求对象): function hrce(key) { var about = { Version: 0.1, Author: "AAA", Created: "Spring 2014", Updated: "March 2014" }; if (key) { this.xhr =
function hrce(key) {
var about = {
Version: 0.1,
Author: "AAA",
Created: "Spring 2014",
Updated: "March 2014"
};
if (key) {
this.xhr = "";
this.xhrdata = "";
this.xhrmethod = "";
this.xhrurl = "";
this.xhrquery = "";
//init with the current avaliable information
this.key = key;
this.protocol = "http:" === document.location.protocol ? "http://" : "https://";
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr = new XMLHttpRequest();
}
else {// code for IE6, IE5
this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return this;
} else {
// No 'id' parameter was given, return the 'about' object
return about;
}
};
hrce.prototype = {
load: function() {
if (this.xhr && this.xhr != "" && this.key && this.key != "") {
this.xhrdata = [{"access_key": this.key}];
this.xhrurl = this.protocol + "localhost/hrce/v1/action/hsio/";
this.xhr.onreadystatechange = this.initilizer();
this.xhr.open("POST", this.xhrurl, true);
this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.xhrquery = "access_key=" + this.key;
this.xhr.send(this.xhrquery);
}
return this;
},
initilizer: function() {
if (this.xhr.readyState == 4 && this.xhr.status == 200)
{
console.log(this.xhr.responseText);
}
}
};
以下是我的库函数:
function hrce(key) {
var about = {
Version: 0.1,
Author: "AAA",
Created: "Spring 2014",
Updated: "March 2014"
};
if (key) {
this.xhr = "";
this.xhrdata = "";
this.xhrmethod = "";
this.xhrurl = "";
this.xhrquery = "";
//init with the current avaliable information
this.key = key;
this.protocol = "http:" === document.location.protocol ? "http://" : "https://";
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr = new XMLHttpRequest();
}
else {// code for IE6, IE5
this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
return this;
} else {
// No 'id' parameter was given, return the 'about' object
return about;
}
};
hrce.prototype = {
load: function() {
if (this.xhr && this.xhr != "" && this.key && this.key != "") {
this.xhrdata = [{"access_key": this.key}];
this.xhrurl = this.protocol + "localhost/hrce/v1/action/hsio/";
this.xhr.onreadystatechange = this.initilizer();
this.xhr.open("POST", this.xhrurl, true);
this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.xhrquery = "access_key=" + this.key;
this.xhr.send(this.xhrquery);
}
return this;
},
initilizer: function() {
if (this.xhr.readyState == 4 && this.xhr.status == 200)
{
console.log(this.xhr.responseText);
}
}
};
现在,如果我调用例如:hrce(“f07c7156”).load()代码>Ajax调用成功,但它没有调用mythis.xhr.onreadystatechange=this.initilizer()代码>调用加载原型函数。有什么问题吗
您没有将函数用作构造函数
您没有为onreadystatechange分配绑定函数或传递闭包
对于第一个您必须决定的问题,是希望函数返回一个对象,还是希望函数作为构造函数工作。如果需要构造函数,请参见下面的示例代码(我将函数名大写,因为构造函数函数应该以大写开头)
对于第二个,您必须传递闭包或使用bind,我在下面的示例中使用传递闭包
function Hrce(key) {
var about = {
Version: 0.1,
Author: "AAA",
Created: "Spring 2014",
Updated: "March 2014"
};
if (key) {
this.xhr = "";
this.xhrdata = "";
this.xhrmethod = "";
this.xhrurl = "";
this.xhrquery = "";
//init with the current avaliable information
this.key = key;
this.protocol = "http:" ===
document.location.protocol ? "http://" : "https://";
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
this.xhr = new XMLHttpRequest();
}
else {// code for IE6, IE5
this.xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//constructor funcitons do not need to return this
return this;
} else {
// No 'id' parameter was given, return the 'about' object
return about;
}
}
;
Hrce.prototype = {
load: function() {
if (this.xhr && this.xhr != "" && this.key && this.key != "") {
this.xhrdata = [{"access_key": this.key}];
this.xhrurl = this.protocol + "localhost/hrce/v1/action/hsio/";
//note that initilizer returns a function that has a closure
// scope with the current instance
this.xhr.onreadystatechange = this.initilizer(this);
this.xhr.open("POST", this.xhrurl, true);
this.xhr
.setRequestHeader("Content-type"
, "application/x-www-form-urlencoded");
this.xhrquery = "access_key=" + this.key;
this.xhr.send(this.xhrquery);
}
return this;
},
initilizer: function(me) {
//returning a function used as closure
// the variable me is the current instance of Hrce
return function(){
if (me.xhr.readyState == 4 && me.xhr.status == 200)
{
console.log(me.xhr.responseText);
}
}
}
};
var connector = new Hrce("f07c7156");
connector.load();
可以找到有关构造函数、原型以及this
变量表示的内容的更多信息。应该是this.xhr.onreadystatechange=this.initilizer代码>,引用函数,而不是调用它!您使用的模式不能很好地与这种功能配合使用,在这种模式中,您需要一直访问XHR对象,并且您将其拆分为原型等等。当前问题的解决方案是使用bind
、call
或apply
更改this
的值,或者引用回调中的对象,而不是this
。类似这样的东西…@dollarVar-没错,但我认为call()也会立即调用该函数,而不是在事件发生时,所以可能需要一个匿名函数,比如->你说的“请求成功了”但“它不工作”是什么意思?@HMR的固溶体解决了吗?太好了!这就是我要找的,谢谢你的回答,特别是你的回答中的链接,你在其中介绍了原型。嘿,做得很好(加上一;)。一开始,他用new
来称呼它,因为整个事情不会像这样运作(他想先对此发表评论)。然而,你知道为什么最后没有“工作”吗?@dollarVar代码可以工作,但是站点不允许POST请求或标题。您可以在firebug或Chrome中看到请求失败(按F12)。将帖子更改为this.xhr.open(“GET”
),并注释掉this.xhr.setRequestHeader("Content-t…
然后它就工作了。他的代码基本上和我的一样,但他创建了一个匿名函数的闭包,可以捕获你从未使用过的大变量。@HMR我还有一个问题。如果我使用你的实现。这里是小提琴:。现在我的问题是我正在调用Hrce的track函数。在你的回答中,我设置了初始化器函数中的cookie。我在加载后立即调用track,但它没有得到cookie。但是如果我单独在控制台上调用track,它会工作。缺少什么?我没有正确使用继承吗?@PHPSeeker快速查看代码,让我认为您假设xhr请求是同步的。当您调用load xhr请求时但当您调用track时,加载请求几乎肯定尚未完成,因此cookie尚未设置。您可以使用回调、承诺或使用中介来解决此问题。最简单的方法是回调,最可维护的方法是承诺或中介,但这有点复杂。