Skip to content

Instantly share code, notes, and snippets.

@avk
Created December 9, 2011 00:48
Show Gist options
  • Save avk/1449532 to your computer and use it in GitHub Desktop.
Save avk/1449532 to your computer and use it in GitHub Desktop.
Titanium Mobile properly serializing JSON into a query string
var url = "http://localhost:3000";
var params = {
foo: 'bar',
stuff: [1,2,3]
};
var xhr = Ti.Network.createHTTPClient({
onload: function() {
alert("load!");
},
onerror: function() {
alert("error!");
}
});
xhr.open('POST', url);
xhr.setRequestHeader("Content-Type","application/json");
params = JSON.stringify(params);
xhr.send(params);
// parameters from Rails log:
// Parameters: {"stuff"=>[1, 2, 3], "action"=>"index", "foo"=>"bar", "controller"=>"home"}
// NOTE:
// parameters will be garbled without the application/json Content-Type
// parameters will also be garbled without JSON.stringify, even though the Titanium docs say that xhr.send() should do this automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment