简体   繁体   English

将JSON编码为Backbone.Model.save的URL参数(patch = true)

[英]Encoding JSON to URL params for Backbone.Model.save (patch = true)

I'm having some trouble correctly forming a Backbone.Model.save call. 我在正确形成Backbone.Model.save调用方面遇到了一些麻烦。 The web service I'm calling consumes URL parameters, but what I have in Javascript is an object of changed fields. 我正在调用的Web服务使用URL参数,但我在Javascript中拥有的是更改字段的对象。 For example, I have the object {foo: 'bar', yar: 'har'} , and I want Backbone.Model.save to send a patch request to a URL like http://server/path/to/service?foo=bar&yar=har 例如,我有对象{foo: 'bar', yar: 'har'} ,我希望Backbone.Model.save将补丁请求发送到http://server/path/to/service?foo=bar&yar=har这样的URL http://server/path/to/service?foo=bar&yar=har

Sounds simple, right? 听起来很简单吧? It's giving me a bunch of trouble anyway. 无论如何,它给了我一堆麻烦。 Here's what I've got so far (which doesn't work; I have success/error callbacks, too, but I don't think those are important for the question): 这是我到目前为止所做的事情(这不起作用;我也有成功/错误回调,但我不认为这些对于这个问题很重要):

object = 
    foo: 'bar', 
    yar: 'har'

model.save object,
    patch: true

I've tried some other options, too: 我也尝试过其他一些选择:

model.save object,
    patch: true
    emulateJSON: true

This set contentType to "application/x-www-form-urlencoded", which is good, but the data sent in the ajax request by Backbone.sync was: {model: "{"foo": "bar", "yar": "har"}". 这将contentType设置为“application / x-www-form-urlencoded”,这很好,但是Backbone.sync在ajax请求中发送的数据是:{model:“{”foo“:”bar“,”yar“ :“har”}“。 The service got that and has no idea what to do with a "model" property. 该服务得到了,并不知道如何处理“模型”属性。

model.save object,
    patch: true
    contentType: "application/x-www-form-urlencoded"

This just codes object as a string and stuffs that into options.data . 这只是将object编码为字符串并将其填充到options.data Again, the service doesn't know what to do with it. 同样,该服务不知道如何处理它。

Any other ideas on how I can get this to conform to my service's spec? 关于如何使其符合我的服务规范的任何其他想法? I can make the ajax call myself and update the model (and the collection it belongs to) myself, but I'd really rather not do that. 我可以自己做ajax调用并自己更新模型(以及它所属的集合),但我真的不愿意这样做。 An ajax request that works for me is: 对我有用的ajax请求是:

$.ajax
    url: "http://server/path/to/service"
    type: "PATCH"
    data: object

Update: The reason my two earlier options didn't work is clear in Backbone.js itself: 更新:我的两个早期选项不起作用的原因在Backbone.js本身很清楚:

// Ensure that we have the appropriate request data.
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
  params.contentType = 'application/json';
  params.data = JSON.stringify(options.attrs || model.toJSON(options));
}

// For older servers, emulate JSON by encoding the request into an HTML-form.
if (options.emulateJSON) {
  params.contentType = 'application/x-www-form-urlencoded';
  params.data = params.data ? {model: params.data} : {};
}

Looking at this, I thought maybe if I stuffed the object into object into options.data and sent in empty attributes, perhaps it'd work: 看着这个,我想也许如果我将对象填充到options.data并将其发送到空属性中,也许它可以工作:

model.save {},
    patch: true
    data: object

Apparently this tried to PATCH an option "[object Object]". 显然,这试图修补一个选项“[object Object]”。 I guess it did a stringify of the object... somewhere... but this may be close to the right answer? 我想它确实是对象的字符串化...某处...但这可能接近正确的答案?

It looks like what I was looking for is the processData option to jQuery.ajax . 看起来我正在寻找的是jQuery.ajaxprocessData选项。 Backbone.sync does the following by default: Backbone.sync默认执行以下操作:

// Don't process data on a non-GET request.
if (params.type !== 'GET' && !options.emulateJSON) {
  params.processData = false;
}

Thus, it wasn't processing the object into URL parameters for me. 因此,我没有将对象处理为URL参数。 (jQuery API) (jQuery API)

So, a working bit of code would be: 因此,一段代码将是:

model.save {},
    patch: true
    data: object
    processData: true

In truth, I may not be using Backbone.Model correctly overall... but, at least it's working. 事实上,我可能没有正确地使用Backbone.Model ...但是,至少它是有效的。 :P :P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM