如何在ajax中传递上下文
How to pass context to done in ajax
以下是场景:
getSomething: function(){
var gotValue = sendAjax();
gotValue.done(function(response){
console.log(response); //How to pass context of object to call getSomethingElse
//getSomethingElse();
});
},
getSomethingElse : function(){
}
我想在ajax 的done方法中调用getSomethingElse函数
使用$this=this;
,因为函数中的this
没有引用对象。
getSomething: function()
{
var $this = this;
var gotValue = sendAjax();
gotValue.done(function(response){
console.log(response);
$this.getSomethingElse();
});
},
getSomethingElse : function()
{
}
或者您可以使用$.proxy
gotValue.done($.proxy(function(response){
console.log(response);
this.getSomethingElse();
},this));
使用对当前上下文的引用:
getSomething: function () {
var self = this;
sendAjax().done(function (response) {
self.getSomethingElse(response);
});
// or even shorter: sendAjax().done(self.getSomethingElse);
},
getSomethingElse: function () {}
使用绑定方法:
getSomething: function () {
sendAjax().done(function (response) {
this.getSomethingElse(response);
}.bind(this));
},
getSomethingElse: function () {}
试试这个:
var someHelper = {
getSomething : function() {
var gotValue = sendAjax();
//keep a reference to the obj, normally "this", or "someHelper"
var self = this;
gotValue.done(function(response) {
console.log(response); //How to pass context of object to call getSomethingElse
//getSomethingElse();
//call getSomethingElse
self.getSomethingElse();
});
},
getSomethingElse : function() {
}
}
相关文章:
- 将函数的上下文应用于javascript变量
- 无法在通过jQuery的ajax加载的页面中执行javascript
- 如何通过ajax刷新JSF填充的javascript变量
- 如何在php文件中获取$.post-ajax传递的值
- Replacing $ .ajax?
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 如何在ajax中传递上下文
- ajax数据选项中的此上下文
- Jquery - ajax 回调的上下文不遵循提供的上下文参数
- 如何避免在 js/ajax 脚本中对应用程序上下文路径进行硬编码
- AJAX:无法通过上下文参数将对象传递到ajaxSuccess函数中
- jQuery上下文菜单中的AJAX加载的内容
- 对于原型ajax,什么是使用上下文的等价
- 实现位置.双向绑定到AJAX记录上下文的散列
- 如何在django模板更改后通过AJAX调用更新加载上下文变量
- 如何获得.js文件上下文jQuery $.接收时不执行Ajax
- 如何正确地为ajax做上下文预加载器
- Jquery上下文未从ajax请求返回现有元素
- 在Flask中使用AJAX添加上下文变量
- 如何正确使用“上下文”?在AJAX调用中