【问题标题】:backbone save fires error when response is 200当响应为 200 时,主干保存会触发错误
【发布时间】:2013-10-12 19:42:13
【问题描述】:

所以这里没有什么特别的(这真的让我很烦)。 我很确定我缺少一些东西。

所以这是相对的代码:

    _me.attributes.answers.push(oAnswer);
    _me.save(null,{
        success : function(){
            console.log('saved the answer');
            q = new q.Questions() ;
            q.fetch({
                success : function(data){
                    console.log('reloading the template with this data');
                    console.log(data)
                    this.render({model:data , el:this.$el})
                }
            })
        },
        error : function(model , response , x){
            console.log('error')
            console.log(response)
            console.log(x)
        }
    });

现在我可以在控制台中看到已发出请求 (PUT) 并且服务器响应为 200 (OK)

但我仍然收到error 记录,当我尝试记录响应时,我得到了这个

readyState
    4

responseText
    "OK"

status
    200

statusText
    "OK"

仍然主干只是为 _me.save 触发 error 处理程序,这是为什么呢?

【问题讨论】:

  • 所以你得到的错误不是_me.save,而是q.fetch
  • 你是从你的服务器返回一个空的身体吗?那是invalid response for jQuery。另一种可能性是:如果您从不同的域拨打电话,您的浏览器可能会尝试为您拨打CORS
  • @glortho 是的,我在 _me.save 上收到错误,这是唯一的错误处理程序。
  • @j03w ,实际上我使用的是nodejs express,我发送的响应如下res.send(200)
  • @HusseinNazzal 所以听起来你需要改变这个:“仍然主干只是触发 q.fetch 的错误处理程序,为什么会这样?”

标签: javascript backbone.js


【解决方案1】:

根据解析函数,您的服务器响应无效。保存时尝试将“parse: false”传递给您的模型选项

model.save(attrs, {
  success: function() { ... },
  parse  : false
});

【讨论】:

  • 这个问题早就解决了。感谢您花时间分享您所知道的。希望有人能从中受益。 :)
猜你喜欢
  • 2013-03-02
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多