Javascript Backbone.js 获取参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6659283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Backbone.js fetch with parameters
提问by Shawn Mclean
Following the documentation, I did:
按照文档,我做了:
var collection = new Backbone.Collection.extend({
model: ItemModel,
url: '/Items'
})
collection.fetch({ data: { page: 1} });
the url turned out to be: http://localhost:1273/Items?[object%20Object]
网址原来是: http://localhost:1273/Items?[object%20Object]
I was expecting something like http://localhost:1273/Items?page=1
我期待着类似的东西 http://localhost:1273/Items?page=1
So how do I pass params in the fetch method?
那么如何在 fetch 方法中传递参数呢?
回答by Joe
changing:
改变:
collection.fetch({ data: { page: 1} });
to:
到:
collection.fetch({ data: $.param({ page: 1}) });
So with out over doing it, this is called with your {data: {page:1}}
object as options
因此,如果不这样做,这将与您的{data: {page:1}}
对象一起调用options
Backbone.sync = function(method, model, options) {
var type = methodMap[method];
// Default JSON-request options.
var params = _.extend({
type: type,
dataType: 'json',
processData: false
}, options);
// Ensure that we have a URL.
if (!params.url) {
params.url = getUrl(model) || urlError();
}
// Ensure that we have the appropriate request data.
if (!params.data && model && (method == 'create' || method == 'update')) {
params.contentType = 'application/json';
params.data = JSON.stringify(model.toJSON());
}
// For older servers, emulate JSON by encoding the request into an HTML-form.
if (Backbone.emulateJSON) {
params.contentType = 'application/x-www-form-urlencoded';
params.processData = true;
params.data = params.data ? {model : params.data} : {};
}
// For older servers, emulate HTTP by mimicking the HTTP method with `_method`
// And an `X-HTTP-Method-Override` header.
if (Backbone.emulateHTTP) {
if (type === 'PUT' || type === 'DELETE') {
if (Backbone.emulateJSON) params.data._method = type;
params.type = 'POST';
params.beforeSend = function(xhr) {
xhr.setRequestHeader('X-HTTP-Method-Override', type);
};
}
}
// Make the request.
return $.ajax(params);
};
So it sends the 'data' to jQuery.ajaxwhich will do its best to append whatever params.data
is to the URL.
因此,它将“数据”发送到jQuery.ajax,它会尽最大努力将任何params.data
内容附加到 URL。
回答by Jimchao
You can also set processData to true:
您还可以将 processData 设置为 true:
collection.fetch({
data: { page: 1 },
processData: true
});
Jquery will auto process data object into param string,
Jquery 将数据对象自动处理为参数字符串,
but in Backbone.sync function, Backbone turn the processData off because Backbone will use other method to process data in POST,UPDATE...
但是在 Backbone.sync 函数中,Backbone 将 processData 关闭,因为 Backbone 将使用其他方法处理 POST、UPDATE 中的数据...
in Backbone source:
在主干源中:
if (params.type !== 'GET' && !Backbone.emulateJSON) {
params.processData = false;
}
回答by peponline
Another example if you are using Titanium Alloy:
另一个例子,如果您使用钛合金:
collection.fetch({
data: {
where : JSON.stringify({
page: 1
})
}
});
回答by Walter von Entferndt
try {
// THIS for POST+JSON
options.contentType = 'application/json';
options.type = 'POST';
options.data = JSON.stringify(options.data);
// OR THIS for GET+URL-encoded
//options.data = $.param(_.clone(options.data));
console.log('.fetch options = ', options);
collection.fetch(options);
} catch (excp) {
alert(excp);
}