Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 主干:如何通过fetch传递参数?_Backbone.js - Fatal编程技术网

Backbone.js 主干:如何通过fetch传递参数?

Backbone.js 主干:如何通过fetch传递参数?,backbone.js,Backbone.js,我已经阅读了有关Stackoverflow的所有帖子,并尝试了以下方法: 1) 添加$.param({})包装器 messages.fetch({ data: $.param({ limit: 14 }), }); (二) 将traditional设置为true messages.fetch({ data: { limit: 14 }, traditional: true }); messages.fetch({ data: { limit

我已经阅读了有关Stackoverflow的所有帖子,并尝试了以下方法:

1) 添加
$.param({})
包装器

  messages.fetch({
    data: $.param({ limit: 14 }),
  });
(二) 将
traditional
设置为
true

  messages.fetch({
    data: { limit: 14 },
    traditional: true
  });
  messages.fetch({
    data: { limit: 14 },
    processData: true,
  });
(三) 将
processData
设置为
true

  messages.fetch({
    data: { limit: 14 },
    traditional: true
  });
  messages.fetch({
    data: { limit: 14 },
    processData: true,
  });


尽管如此,这些方法都不起作用。这里有我遗漏的东西吗?

对于我认为您要做的事情,您需要在模型/集合上使用“url”属性。Fetch是一种GET类型,而不是PUT或POST,因此您不会向服务器发送“数据”。请尝试以下操作:

var MyModel = Backbone.Model.extend({

    url = 'api/somewhere/great'

    fetch: function(options) {
        options = options || {
            data: {
                limit: 14
            }
        };

        /*
            signature method of sync: Backbone.sync = function (method, model, options)
        */
        return this.sync("create", this, options);
    }
});
请注意,$.param({limit:14})返回“limit=14”关键是,这不是服务器将要理解的有效序列化对象

如果“fetch”方法是“HttpGet”

现在,如果您想要HttpPut或HttpPost,那么您可以覆盖“fetch”并执行如下操作:

var MyModel = Backbone.Model.extend({

    url = 'api/somewhere/great'

    fetch: function(options) {
        options = options || {
            data: {
                limit: 14
            }
        };

        /*
            signature method of sync: Backbone.sync = function (method, model, options)
        */
        return this.sync("create", this, options);
    }
});
参考文献:


尽管如此,这些方法都不起作用。
那么到底发生了什么?您不需要使用
param
processData
traditional
?我假设消息是主干网的一个实例?当您获取like
messages.fetch({data:{limit:14}})时
您对服务器的请求应该类似于
api/messages?limit=14
。您可以显示该
GET
请求的url吗?如果您检查调试器控制台的导航选项卡,您可以轻松地检查该url。