Backbone 获取 url 未按预期格式化
Backbone fetch url not formatting as expected
它与 backbone fetch URL data formatting error 类似,只是在尝试回答时 URL 看起来像 http://localhost?site:[Object%20Object]
我的数据:
options.data = { name:companyName}
公司名称的值类似于 "One, Two, Three"
。如果不执行 encodeURIComponent
,查询看起来像 http://localhost?name=One%2C+Two%2C+Three
.
我希望它看起来像 http://localhost?name=One%2C%20Two%2C%20Three
(注意 URL 中的 %20
和 +
的删除)。
为了清楚起见,我的代码看起来像这样:
sync: function(method, model, options) {
switch(method){
case "read": {
options.data = { name:options.data.name} //I only want to pass in a subset of properties for the query.
//options.data = {encodeURIComponent(options.data.name)} //this returns [Object%20Object]
}
break;
default:
break;
}
Backbone.sync.apply(this, arguments);
}
我已经尝试 processData:
在这两种情况下都是 false
或 true
。
更新
我目前正在通过执行以下操作来解决此问题:
sync:function(method, model, options){
switch(method){
case "read":
options.url = this.url+"?name="+options.data.name
break;
default:
break;
}
Backbone.sync.apply(this, arguments);
}
这让我得到了预期的结果我简直不敢相信这就是答案而且 backbone 无法处理 get 请求中的连续特殊字符
像下面这样的简单提取会自动使用 /test?name=One%2C%20Two%2C%20Three
。 encodeURIComponent
不需要。
collection.fetch({
data: {
name: "One, Two, Three"
}
});
var Collection = Backbone.Collection.extend({
url: "test"
});
var collection = new Collection();
collection.fetch({
data: {
name: "One, Two, Three"
}
});
// request to /test?name=One%2C%20Two%2C%20Three
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData
option to prevent this automatic processing. Object must be Key/Value pairs.
如果您不能将它作为选项传递给 fetch
函数,您应该覆盖集合(或模型)的 fetch
函数而不是 sync
。
var Collection = Backbone.Collection.extend({
url: "test",
fetch: function(options) {
options = options || {};
// example, extending the data object.
options.data = _.extend({}, options.data, { name: this.companyName });
return Collection.__super__.fetch.call(this, options);
}
});
它与 backbone fetch URL data formatting error 类似,只是在尝试回答时 URL 看起来像 http://localhost?site:[Object%20Object]
我的数据:
options.data = { name:companyName}
公司名称的值类似于 "One, Two, Three"
。如果不执行 encodeURIComponent
,查询看起来像 http://localhost?name=One%2C+Two%2C+Three
.
我希望它看起来像 http://localhost?name=One%2C%20Two%2C%20Three
(注意 URL 中的 %20
和 +
的删除)。
为了清楚起见,我的代码看起来像这样:
sync: function(method, model, options) {
switch(method){
case "read": {
options.data = { name:options.data.name} //I only want to pass in a subset of properties for the query.
//options.data = {encodeURIComponent(options.data.name)} //this returns [Object%20Object]
}
break;
default:
break;
}
Backbone.sync.apply(this, arguments);
}
我已经尝试 processData:
在这两种情况下都是 false
或 true
。
更新
我目前正在通过执行以下操作来解决此问题:
sync:function(method, model, options){
switch(method){
case "read":
options.url = this.url+"?name="+options.data.name
break;
default:
break;
}
Backbone.sync.apply(this, arguments);
}
这让我得到了预期的结果我简直不敢相信这就是答案而且 backbone 无法处理 get 请求中的连续特殊字符
像下面这样的简单提取会自动使用 /test?name=One%2C%20Two%2C%20Three
。 encodeURIComponent
不需要。
collection.fetch({
data: {
name: "One, Two, Three"
}
});
var Collection = Backbone.Collection.extend({
url: "test"
});
var collection = new Collection();
collection.fetch({
data: {
name: "One, Two, Three"
}
});
// request to /test?name=One%2C%20Two%2C%20Three
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js"></script>
It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See
processData
option to prevent this automatic processing. Object must be Key/Value pairs.
如果您不能将它作为选项传递给 fetch
函数,您应该覆盖集合(或模型)的 fetch
函数而不是 sync
。
var Collection = Backbone.Collection.extend({
url: "test",
fetch: function(options) {
options = options || {};
// example, extending the data object.
options.data = _.extend({}, options.data, { name: this.companyName });
return Collection.__super__.fetch.call(this, options);
}
});