typescript的ajax同步,TypeScript - 在TypeScript中实现的Ajax类

这篇博客讨论了在JavaScript中定义Ajax类时遇到的问题,即`readyState`、`status`和`responseText`属性不在类实例上,而应该在`XMLHttpRequest`对象上。作者提供了修正后的TypeScript代码,确保了正确引用`XMLHttpRequest`对象的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

class Ajax {

url: string;

xmlData: string;

mode: bool;

response: string;

objHttpReq:any;

constructor (postUrl: string, postXml: string, postMode: bool) {

this.url = postUrl;

this.xmlData = postXml;

this.mode = postMode;

this.objHttpReq = new XMLHttpRequest();

this.objHttpReq.mode = this.mode;

this.objHttpReq.onreadystatechange = this.OnRStateChange;

this.objHttpReq.open("Post", this.url, this.mode);

this.objHttpReq.send(this.xmlData);

}

OnRStateChange(){

if (this.readyState==4 && this.status==200)

//here this refers to Ajax

{

//alert(xmlhttp.status);

if(this.mode == false)

{

alert(this.responseText);

}

else

{

alert(this.responseText);

}

}

}

}

遵守上面的代码

var Ajax = (function() {

function Ajax(postUrl, postXml, postMode) {

this.url = postUrl;

this.xmlData = postXml;

this.mode = postMode;

this.objHttpReq = new XMLHttpRequest();

this.objHttpReq.mode = this.mode;

this.objHttpReq.onreadystatechange = this.OnRStateChange;

this.objHttpReq.open("Post", this.url, this.mode);

this.objHttpReq.send(this.xmlData);

}

Ajax.prototype.OnRStateChange = function() {

if(this.readyState == 4 && this.status == 200) {

//here this refers XMLHttpRequest object – works fine

if(this.mode == false) {

alert(this.responseText);

} else {

alert(this.responseText);

}

}

};

return Ajax;

})();

问题上面打字稿代码表明错误的JavaScript,因为阿贾克斯类没有readyState的,状态和性能的responseText。在TypeScript中编写Ajax类的正确代码应该是什么?

2013-01-21

skgyan

+0

你摸不着头脑? –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值