I am building a phonegap application that interfaces with a web service I've set up. Having a strange problem that I cannot explain. The same exact code works properly on Android but fails on iPhone. It's just jQuery.
Here is the code:
$.ajax({
开发者_如何学编程 url: app_domain + '/sessions',
type: 'POST',
dataType: 'json',
data: {
session: {
email: u_email,
password: u_password
},
mobile: '1' },
async: true,
cache: false,
success: function(data){
// do cool stuff here
},
error: function(){
// complain here
}
})
And here is the part I cannot figure out. When I look in my server logs to try and figure out what is going wrong with this POST request i see the following for android:
...
Processing by SessionsController#create as JSON
...[SUCCESS]
and for the iPhone request:
...
Processing by SessionsController#create as HTML
...[FAIL]
The difference being JSON (correctly) for POST originating from Android device and HTML (incorrect) for POST originating from iPhone4.
What gives? Any ideas? Thanks!
p.s. this was originally written as a jquery $.post() function and it had the same outcome. I rewrote it as $.ajax() for the increased specificity...
You probably need to modify the outgoing HTTP headers to indicate that you are in fact sending JSON. Add headers to your ajax map:
headers: {'Content-Type': 'application/json'}
精彩评论