jquery - 有没有办法让 JQuery ajax 成功函数访问它所包含的对象?

标签 jquery ajax

我有这样的 JavaScript:

function Cat() { 

  this.meow = function() { // meow };

  $.ajax( do AJAX call, success: this.meow(); );

}

var TopCat = new Cat();

这不起作用,因为“this”在成功函数的上下文中没有意义。有没有优雅的解决方案?

最佳答案

您正在寻找 ajax 方法的 context 参数。
它允许您设置调用所有回调的上下文。

function Cat() { 
    this.meow = function() { // meow };
    $.ajax({
        context: this, 
        success: function() { this.meow(); } 
    });    
}

关于jquery - 有没有办法让 JQuery ajax 成功函数访问它所包含的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5378622/

相关文章:

jquery - 使用 jquery 或 javascript 更改 CSS 根变量

javascript - jQuery fadeOut 和 fadeIn 背景图像

javascript - 阻止 'mouseenter'回调函数直到 'highlight'效果完成

javascript - 根据Angular JS中的ID将一页数据发送到另一个寻呼机

php - 我无法使用 AJAX (XMLHttpRequest) 获取 responseXML

javascript - 如何在特定单元格或表格的td中添加多个输入?

javascript - 显示到 ListView 问题

jquery - Internet Explorer 10 不发送 Ajax Post 数据

jquery - Internet Explorer 8 的布局/Javascript 问题

ajax - 将 beforeSend 添加到主干 ajax 调用