GET request with params in Titanium to a Rails backend
I’m currently developing an iPhone app with Appcelerator’s Titanium that communicates with a Rails backend. Doing so I found a few problems and here is how I solved them. This is the first time I work with Titanium, so don’t expect something fancy ; )
Ajax GET request with params:
We’ll use Titanium.Network.HTTPClient
, if you have never used it I suggest that you take a look a this awesome example in the Titanium wiki that perfectly covers the basics.
Say our rails app is a blog at the domain: http://www.railsblog.com, and we want to get the all posts that are written in Spanish by the author ariera. My first attempt to do so looked like this:
It took me some time to understand why, but that code doesn’t work as you would expect it to work. Although we clearly specified that we wanted to do a GET
request it will do it as a POST
. The reason why this happens is that when you pass any parameter to the send method it automatically converts the request into POST
.
My solution was to manually construct the url with parameters using encondeURI
:
And that finally worked. But was that all? Of course not ;-P
Titanium enable your Rails app:
When testing the iPhone app with my local copy of this railsblog I got the most weird succinct not-descriptive error Rails ever gave me:
that NoMethodError (undefined method 'ref' for nil:NilClass)
error drove me crazy for a while. Apparently this is due to a mix of user agent and Mime Types handled incorrectly by Rails. Following this stackoverflow answer I did this: