Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

POST a JSON array with Titanium Mobile

I must be trying too hard or there seems to be an error with the way JSON is being POSTed.

The following structure works for example with jQuery:

xhr.send({
    'object': { 'child1':'value', 'child2':'value' }
});

But it's not nesting correctly with Titanium Mobile.

My web app keeps seing something like object = "{\'child1\' ...

Am I doing something wrong or is this is a platform bug?

Thanks for any help.

— asked September 10th 2010 by Alexandre Loureiro Solleiro
  • http
  • json
  • mobile
  • network
  • post
  • xhr
0 Comments

2 Answers

  • Are you sure you can send a nested json over http?

    You may try this:

    xhr.send({
        object: { child1:'value', child2:'value' }
    });
    

    or if it doesn't work, you can try to stringify the json, send it as a simple string variable and parse it serverside.

    — answered September 11th 2010 by Dan Tamas
    permalink
    1 Comment
    • Thank you for your response.

      Yes you can send JSON over HTTP, but it turns out I needed to make sure it was JSON I was sending!

      This now works for me (it also works if keys are of the 'child1' sort):

      var postData = {
          object: { child1:'value', child2:'value' }
      }
      xhr.send(JSON.stringify(postData));
      

      — commented September 11th 2010 by Alexandre Loureiro Solleiro
  • function setTask(idUser, idService, Reason){
            xhrPost.open('POST', myPost);
            xhrPost.send({'User':idUser,'c':'mark','ServiceTick':idService,'Reason':Reason});
    };
    

    works so in my case
    Andrea

    — answered September 11th 2010 by Andrea S
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.