简体   繁体   English

YUI3 IO实用程序是否可以根据给定的内容类型标头值自动序列化数据?

[英]Can YUI3 IO utility serialize data automatically based on the given content-type header value?

Here is my code: 这是我的代码:

  data[this.getColumn().key] = oNewValue;
  request = Y.io("admin/api/inventory/" + encodeURIComponent(this.getRecord().getData()._id), {
    method: "PUT",
    headers: { 'Content-Type': 'application/json'},
    data: data,
    on: {
      success: function () {
        fnCallback(true, oNewValue);
      },
      failure: function () {
        fnCallback(false, oNewValue);
      }
    }
  });

Where the data object equals {price: 1200} . 数据对象等于{price: 1200}

Right now, {price: 1200} is serialized as price=1200 (I do not even know how it happens). 现在, {price: 1200}被序列化为price=1200 (我什至不知道它是如何发生的)。 Obviously, this is not JSON. 显然,这不是JSON。

Now I can manually stringify data to a json string, but I was wondering whether YUI has a facility to stringify data to json automatically. 现在我可以手动将data字符串化为json字符串,但是我想知道YUI是否具有将数据自动字符串化为json的功能。 Ideally, I would like just give data and have the json content-type set as well as the data stringified by YUI for me. 理想情况下,我只想提供data并设置json内容类型以及YUI为我字符串化的数据。

Is it possible? 可能吗?

Y.io has no inherent knowledge that you want JSON data to be sent, so it will transform any object into essentially the same thing you would get if you sent a form along (form encoded). Y.io不了解您要发送JSON数据的内在知识,因此它将把任何对象转换成与您一起发送表单(表单编码)所获得的本质上相同的东西。

If you want to get into automatic serialization, I would recommend taking a look at Y.Model and Y.ModelSync.REST. 如果要进入自动序列化,我建议您看一下Y.Model和Y.ModelSync.REST。 It handles all the IO for you, and simplifies code a great deal. 它为您处理所有IO,并大大简化了代码。

http://yuilibrary.com/yui/docs/model/ http://yuilibrary.com/yui/docs/model/

And specifically: http://yuilibrary.com/yui/docs/model/#model-sync-layers 特别是:http: //yuilibrary.com/yui/docs/model/#model-sync-layers

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

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