通过发布 XML 而不是 JSON 使用 Backbone 调用 RESTful 服务
Call RESTful services using Backbone by posting XML instead of JSON
我有一个 Spring RESTful 服务,例如
@RequestMapping(value = "/test", method = RequestMethod.POST, headers = { "content-type=application/xml" })
@Transactional(readOnly = false)
public @ResponseBody String saveSubscription(@RequestBody Subscription subscription) {
....
}
我的 UI 有 Backbone 代码就像,
var Subscription = Backbone.Model.extend({
urlRoot :http://localhost:8087/SpringRest/test',
});
$(document).ready(function() {
$('#new-subscription').click(function(ev) {
var subscription = new Subscription({
name : $('#dropdown').val(),
type : $('input[name=type]:checked').val(),
format : $('input[name=format]:checked').val(),
});
subscription.save();
});
通过使用此代码,我调用了 Spring REST 服务。这里Backbonepost订阅数据为JSON.
但我想 post 它作为 XML 然后 spring 解组将在点击服务时发生。
如何在从客户端访问服务之前将 Backbone 模型 JSON 解析为 XML?
您可以通过覆盖 sync
方法来修改 Backbone 同步数据的方式。例如,您的模型定义可能类似于
var Subscription = Backbone.Model.extend({
urlRoot: 'http://localhost:8087/SpringRest/test',
sync: function(method, model, options) {
if ((method !== "create") && (method !== "update"))
return Backbone.sync.apply(this, arguments);
options = options || {};
// what the server responds with
options.dataType = 'xml';
// the content type of your data
options.contentType = 'application/xml';
// what you send, your XML
// you will have to tailor it to what your server expects
var doc = document.implementation.createDocument(null, "data", null);
_.each(this.toJSON(), function(v, k) {
var node = doc.createElement(k);
var text = doc.createTextNode(v);
node.appendChild(text);
doc.documentElement.appendChild(node);
});
options.data = doc;
return Backbone.sync.call(this, method, model, options);
}
});
那会发送
<data>
<name>...</name>
<type>...</type>
<format>...</format>
</data>
我有一个 Spring RESTful 服务,例如
@RequestMapping(value = "/test", method = RequestMethod.POST, headers = { "content-type=application/xml" })
@Transactional(readOnly = false)
public @ResponseBody String saveSubscription(@RequestBody Subscription subscription) {
....
}
我的 UI 有 Backbone 代码就像,
var Subscription = Backbone.Model.extend({
urlRoot :http://localhost:8087/SpringRest/test',
});
$(document).ready(function() {
$('#new-subscription').click(function(ev) {
var subscription = new Subscription({
name : $('#dropdown').val(),
type : $('input[name=type]:checked').val(),
format : $('input[name=format]:checked').val(),
});
subscription.save();
});
通过使用此代码,我调用了 Spring REST 服务。这里Backbonepost订阅数据为JSON.
但我想 post 它作为 XML 然后 spring 解组将在点击服务时发生。
如何在从客户端访问服务之前将 Backbone 模型 JSON 解析为 XML?
您可以通过覆盖 sync
方法来修改 Backbone 同步数据的方式。例如,您的模型定义可能类似于
var Subscription = Backbone.Model.extend({
urlRoot: 'http://localhost:8087/SpringRest/test',
sync: function(method, model, options) {
if ((method !== "create") && (method !== "update"))
return Backbone.sync.apply(this, arguments);
options = options || {};
// what the server responds with
options.dataType = 'xml';
// the content type of your data
options.contentType = 'application/xml';
// what you send, your XML
// you will have to tailor it to what your server expects
var doc = document.implementation.createDocument(null, "data", null);
_.each(this.toJSON(), function(v, k) {
var node = doc.createElement(k);
var text = doc.createTextNode(v);
node.appendChild(text);
doc.documentElement.appendChild(node);
});
options.data = doc;
return Backbone.sync.call(this, method, model, options);
}
});
那会发送
<data>
<name>...</name>
<type>...</type>
<format>...</format>
</data>