mark
mark

Reputation: 62846

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}.

Right now, {price: 1200} is serialized as price=1200 (I do not even know how it happens). Obviously, this is not 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. Ideally, I would like just give data and have the json content-type set as well as the data stringified by YUI for me.

Is it possible?

Upvotes: 0

Views: 473

Answers (1)

jshirley
jshirley

Reputation: 264

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).

If you want to get into automatic serialization, I would recommend taking a look at Y.Model and Y.ModelSync.REST. It handles all the IO for you, and simplifies code a great deal.

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

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

Upvotes: 4

Related Questions