I am encountering an issue where my attempts to post a JSON object to a remote server (Rails) are failing. The POST parameters seem to be converted to a url-encoded string instead of being sent as 'application/json'. Here is an example of what I am trying to do:
appAPI.request.post({
url: "http://mybackend",
postData: {hello: 'world', foo: 'bar'},
onSuccess: function(response) {
console.log("postback succeeded with response: " + response)
},
onFailure: function(httpCode) {
console.log("postback failure: " + httpCode)
},
contentType: 'application/json'
});
However, this results in an HTTP 500 error with the server complaining about a malformed JSON object:
Error occurred while parsing request parameters.
Contents:
MultiJson::LoadError (784: unexpected token at 'hello=world&foo=bar'):
/Users/hammady/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/json/common.rb:148:in `parse'
...
I need help in figuring out how to successfully send the JSON object to my Rails backend without it being converted to a url-encoded string.