【问题标题】:Backbone options with sync is not passing data带同步的主干选项不传递数据
【发布时间】:2016-02-15 20:52:12
【问题描述】:

我正在处理一个主干项目,其中我的 js 文件具有映射到模型的保存功能。

this.model.save({
              success:function(){

},
attrs : attrs,
.
.
.
.

})

在我得到的同步功能的主干模型中

 sync:function(method, model, options){

       if(method == 'update'){
            options.url = 'my url here';
            options.data = JSON.stringify(_.omit(this.attributes,['username','firstname']))
}

    }

如果我检查网络响应,输出(在 chrome 中)是

{'password':'','lastname':''}:

希望您能看到最后一个分号(在大括号之后)与发送的对象一起出现。

我用邮递员测试了没有 : 的响应,它工作正常。所以我得出结论,在我的 PUT 请求被拒绝的数据中添加了分号。

我该如何克服?

【问题讨论】:

    标签: backbone.js


    【解决方案1】:

    这看起来不错:

    var a = {'username': 'username', 'password': 'password', 'firstname': 'firstname', 'lastname': 'lastname'}
    JSON.stringify(_.omit(a,['username','firstname']))
    >>> "{"password":"password","lastname":"lastname"}"
    

    请粘贴更多代码。我想看看完整的同步通话。看一下实现:http://backbonejs.org/docs/backbone.html#section-169

    【讨论】:

    • 我通过指定 content-type:'application/json' 让代码工作
    • @AlaksandarJesusGene 这是默认行为(发送内容类型:应用程序/json),如果这是问题,我认为您没有正确抽象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2015-03-26
    相关资源
    最近更新 更多