我试图通过typescript从成功对象返回值。并且似乎没有得到如何做到这一点。我通过回调函数尝试了它,但它没有成功调用它。基本上我需要将ajax成功或错误数据返回给编辑方法。为了进一步处理它。
class ProductsController {
element:JQuery;
url:string;
token:string;
action:string;
id:number;
data:Object;
constructor(element:string) {
this.element = $(element);
this.url = this.element.attr('href');
this.token = this.element.data('token');
this.id = this.element.data('id');
this.action = this.element.data('action');
};
ajaxRequest(request) {
var self = this;
$.ajax(request).done(function (results) {
self.data = results;
}).fail(function (error) {
self.data = error;
})
return self.data;
}
edit() {
return this.ajaxRequest({
url: this.url,
type: "get",
})
}
update() {}
del() {}
}
$(".product-act").click(function (e) {
e.preventDefault();
var el = new ProductsController(this);
console.log(el.edit());
});