将我自己的回调与 HttpRequest 对象结合使用
我正在编写一个不使用库的 Http 请求(另一个脚本有冲突......)
但是我在对象的范围方面遇到了麻烦。下面是调用脚本,然后是 Ajax_Request 对象。
function loadCard(e) {
var element = e.target;
if($('overlay')) {
return false; //something is already over the layout
}
var card = '/card/'+element.id;
var option = {method:'post', parameters:'test', async:true}
loadOverlay();
var ajax = new Ajax_Request(card, option);
}
//Ajax_Request
function Ajax_Request(url, options) {
if(typeof url !== 'undefined') {
this.url = url;
}
if(typeof options.method !== 'undefined') {
this.method = options.method;
} else {
this.method = 'get';
}
if(typeof options.parameters !== 'undefined') {
this.parameters = options.parameters;
}
if(typeof options.async !== 'undefined') {
this.async = true;
} else {
this.async = false;
}
if(window.XMLHttpRequest) {
this.request = new XMLHttpRequest();
} //check for MS browser
this.makeRequest = function() {
try {
this.request.onreadystatechange = this.checkReadyState;
this.request.open(this.method, this.url, this.async);
if(this.method == 'post') {
this.request.send(this.parameters);
} else {
this.request.send(null);
}
} catch(err) {
alert(err);
}
}
this.setResponse = function(r) {
alert(r)
this.response = r;
}
this.getResponse = function() {
return this.responseText;
}
this.checkReadyState = function(r) {
switch(this.readyState) {
case 4:
//Represents a "loaded" state in which the response has been completely received.
if(this.status == 200) {
this.setResponse(this.responseText)
}
...
}
}
}
我正在尝试设置对属性的响应,以便我的调用对象可以使用它。 但是当我尝试调用 this.setResponse() 时,我收到一个错误,指出它未定义。 如何将 onreadystatechange 回调正确绑定到我的程序?
否则,脚本会正确返回数据,我可以简单地在那里输出它,但我需要更多的灵活性。
谢谢 富有的
I'm writing an Http Request without the use of a library (another script was having conflits...)
But Im having trouble with the scope of my object. Below is the calling script, then the Ajax_Request object follows.
function loadCard(e) {
var element = e.target;
if($('overlay')) {
return false; //something is already over the layout
}
var card = '/card/'+element.id;
var option = {method:'post', parameters:'test', async:true}
loadOverlay();
var ajax = new Ajax_Request(card, option);
}
//Ajax_Request
function Ajax_Request(url, options) {
if(typeof url !== 'undefined') {
this.url = url;
}
if(typeof options.method !== 'undefined') {
this.method = options.method;
} else {
this.method = 'get';
}
if(typeof options.parameters !== 'undefined') {
this.parameters = options.parameters;
}
if(typeof options.async !== 'undefined') {
this.async = true;
} else {
this.async = false;
}
if(window.XMLHttpRequest) {
this.request = new XMLHttpRequest();
} //check for MS browser
this.makeRequest = function() {
try {
this.request.onreadystatechange = this.checkReadyState;
this.request.open(this.method, this.url, this.async);
if(this.method == 'post') {
this.request.send(this.parameters);
} else {
this.request.send(null);
}
} catch(err) {
alert(err);
}
}
this.setResponse = function(r) {
alert(r)
this.response = r;
}
this.getResponse = function() {
return this.responseText;
}
this.checkReadyState = function(r) {
switch(this.readyState) {
case 4:
//Represents a "loaded" state in which the response has been completely received.
if(this.status == 200) {
this.setResponse(this.responseText)
}
...
}
}
}
I'm trying to set the response to a property so my calling object can work with it.
But when I try to call this.setResponse(), I get an error that it's undefined.
How can I tie the onreadystatechange callback to my program properly?
The script otherwise returns the data properly, and I could simply output it right there, but I need a bit more flexibility.
Thanks
Rich
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这种情况发生在您身上,是因为在
checkReadyState
函数中this
实际上表示 XMLHttPRequest 实例,而不是您的 Ajax_Request 对象,因此this.setResponse
未定义。为了引用你的对象的方法,你必须使用一个小技巧:var that = this
。This is happening to you because inside the
checkReadyState
functionthis
actually represents the XMLHttPRequest instance not you Ajax_Request object, thusthis.setResponse
is undefined. In order to reference your object´s method you have to use a little trick:var that = this
.我不确定这是否是问题所在,但您不应该在构造函数中引用
Ajax_Request
。请改用this
。 (this
指的是实际的对象实例,Ajax_Request
指的是对象构造函数。)I'm not sure whether this is the problem, but you shouldn't be referring to
Ajax_Request
within the constructor. Usethis
instead. (this
refers to the actual object instance—Ajax_Request
refers to the object constructor.)在 this.checkReadyState 中,尝试将
this.setResponse(this.responseText)
更改为this.setResponse(this.request.responseText);
。In this.checkReadyState, try changing
this.setResponse(this.responseText)
tothis.setResponse(this.request.responseText);
.