【发布时间】:2019-12-16 05:07:04
【问题描述】:
我创建类。一切正常,但成功功能不重定向。为什么?
class Ajax {
constructor(url, method, dataType, reDirect, alert) {
this.url = url,
this.method = method,
this.dataType = dataType,
this.reDirect = reDirect,
this.alert = alert
}
getAJAX() {
$.ajax({
url: this.url,
method: this.method,
dataType: this.dataType,
success: function(result) {
window.location = this.reDirect
},
error: function(result) {
alert(this.alert)
}
})
}
}
这是我从类中调用方法的方式:
const clearConf = new Ajax('/ConfigurationHistories/ClearList', 'GET', 'text', '/ConfigurationHistories/Index', 'Not clear list.')
clearConf.getAJAX()
【问题讨论】:
-
success处理函数中的this不包含对您的Ajax类的引用。您需要将其缓存在处理程序之外或使用箭头函数(假设您根本不需要支持 IE)。有关详细信息,请参阅副本
标签: javascript jquery object