Request encodes empty objects as empty strings for key and value ('':'')

25 views
Skip to first unread message

jwerre

unread,
Mar 4, 2013, 7:28:15 PM3/4/13
to mootool...@googlegroups.com

I'm trying to swap out jQuery.ajax for mootools.Request. And for some reason the server doesn't receive the same data. This is somewhat troubling because don't want to have to do any extra server-side work.

ajax_request = function(method, url, data, cb) {

  // MOOTOOLS *does not work in some cases*
  new Request({
    method: method,
    url: url,
    data: data,
    headers: {
      'Content-type': 'application/json'
    },
    encoding: 'utf-8',
    onSuccess: function(res) {
      return cb(JSON.decode(res));
    },
  }).send();

  /* OLD JQUERY REQUEST
  $.ajax({
    url: url,
    type: method,
    data: JSON.encode(data),
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    success: function(res) {
      return cb(res);
    }
  });
  */
}

The server logs the following:

jQuery:

{ pos: 1,
  current_num: 1,
  filter: { on: '', key: '' },
  num_records: 25,
  emptyObject: {},
  id: 419 }

MooTools:

{ pos: '1',
  current_num: '1',
  filter: { on: '', key: '' },
  num_records: '25',
  '': '',
  id: 419 }

Mootoos with encoded data:

In order to preserver the emptyObject I JSON.endode(args) but I just get's encoded twice since Request aleady encodes it.

{ '{"pos":1,"current_num":1,"filter":{"by":"","value":""},"emptyObject":{}}': '', id: 419 }

My problem is basically that empty objects are replaced with empty quotes. Any idea why this could be happening and how to fix it? 


Thanks

Rolf Langenhuijzen

unread,
Mar 31, 2013, 9:09:25 AM3/31/13
to mootool...@googlegroups.com
Did you know there's also Request.JSON?
Empty objects are removed from the data, but what was you expecting on the backend? Empty strings are preserved.

Maybe you can tell more about the backend and how you're working with data. Perhaps it can use a cleanup, or perhaps then I understand why you'd want the empty object on the backend side again (is this a js backend?).

Rolf
Reply all
Reply to author
Forward
0 new messages