【问题标题】:Ready state is still 1 though I got the Response尽管我得到了响应,但就绪状态仍然是 1
【发布时间】:2017-08-14 22:50:41
【问题描述】:
          toggleCompletedCheck : function(e) {
            e.stopPropagation();
            e.stopImmediatePropagation();
            var key = $(e.currentTarget).attr("id");
            this.model = todoCollection.findWhere({
                key : key
            });
            this.model.toggle("completed", true);
            this.option.collection = todoCollection.add(this.model);
            var email = this.model.get("email");
            var title = this.model.get("title");
            var key = this.model.get("key");
            var status = this.model.get("status");
            var completed = this.model.get("completed");
            this.updateUserData(email, key, title, completed, status);
            returnValue = this.model.save();
            console.log(returnValue);
        },

函数中的就绪状态仍为 1。我使用的变量是一个窗口对象(returnValue)。当我在控制台(从 chrome 浏览器)中再次打印对象时,它向我显示就绪状态 4 还允许我使用 returnValue.responseText 访问 responseText。我正在使用backbone.js 将输入保存到后端。这将返回已保存的 responseText。但反过来,当我尝试它说未定义时,我无法访问它。如何在此函数中获取我需要的 responseText。

【问题讨论】:

    标签: javascript java ajax backbone.js


    【解决方案1】:

    Backbone 的 model.save() 方法是异步的。它返回一个值(javascript xhr 对象),但请求在返回时未完成。

    要使用已完成的响应,您通常会将 successerror 回调传递给 save 方法 (docs here):

    this.model.save(null, {
        success: function(model, response, options) {
            // do something with the response
        },
        error: function(model, response, options) {
            // do something with the response
        }
    });
    

    当您习惯于 returning 函数的响应时,这可能需要一点调整,但使用回调几乎总是可以实现等效功能。

    【讨论】:

    • 谢谢,它成功了。由于数据不是 JSON。我使用如下。 this.model.save([],{ dataType:"text", success:function(response) { returnValue = response.changed.Saved.xhr.responseText; console.log(returnValue); return returnValue; }, error:function () {} });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2021-07-10
    • 2011-12-02
    • 1970-01-01
    相关资源
    最近更新 更多