简体   繁体   English

我如何在ajax成功函数中为实例变量赋值

[英]How do i assign value to instance variable within ajax success function

I want to assign value to a class instance variable from within ajax success functions, code will explain better what I mean. 我想从ajax成功函数中为类实例变量赋值,代码将更好地解释我的意思。

var SomeClass = function() {
    this.someMethod = function() {
        $.ajax({
            method: 'GET',
            url: 'http://example.com',
            success: function(resp) {
                var js = JSON.parse(resp);
                // I want to assign js object to SomeClass.response instance variable
            };
        });
    };
};

If I try this.response, it obviously doesn't work. 如果我尝试this.response,显然不起作用。 If I assign this to some variable before I make ajax call, It doesn't work either. 如果我在进行ajax调用之前将其分配给某个变量,那么它也不起作用。 I mean this: 我的意思是:

var SomeClass = function() {
    this.someMethod = function() {
        // Asign this to self
        var self = this;
        $.ajax({
            method: 'GET',
            url: 'http://example.com',
            success: function(resp) {
                var js = JSON.parse(resp);
                // I want to assign js object to SomeClass.response instance variable
                self.response = js;  // However it doesn't work
            };
        });
    };
};

I'll appreciate your help!!! 感谢您的帮助!!!

Since AJAX is asynchronous, you can't use someVariable.response until after the AJAX call completes. 由于AJAX是异步的, someVariable.response直到AJAX调用完成后才能使用someVariable.response The appropriate way is to have someMethod take a callback: 适当的方法是让someMethod进行回调:

var SomeClass = function() {
    this.someMethod = function(callback) {
        // Asign this to self
        var self = this;
        $.ajax({
            method: 'GET',
            url: 'http://example.com',
            success: function(resp) {
                var js = JSON.parse(resp);
                // I want to assign js object to SomeClass.response instance variable
                self.response = js;
                callback();
            };
        });
    };
};

Then you would use it like this: 然后,您将像这样使用它:

var someVariable = new someClass;
someVariable.someMethod(function() {
    console.log(someVariable.response);
});

While @Barmar solution will work, i think the best way its to just use promises.. and since you are using jQuery already this is pretty easy. 虽然@Barmar解决方案可以工作,但我认为最好的方法是仅使用Promise ..并且由于您使用的是jQuery,因此这非常容易。 See below: 见下文:

var SomeClass = function() { this.someMethod = function() { return $.ajax({ method: 'GET', url: 'http://example.com' }); }; };

And then you do something like this: 然后您执行以下操作:

var someVariable = new SomeClass();
    someVariable.someMethod().then(function(returnedValue){
        console.log(JSON.parse(returnedValue));
});

I believe that promises is the way to go and since they will be included in ES6... its better to familiarise yourself with the concept. 我相信承诺是要走的路,并且因为它们将被包含在ES6中……所以最好让自己熟悉这个概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM