For some reason, the $.ajax() is not getting called. It is detecting the $("#login").submit() function because I did have alerts in there and it called it.
I am using http://build.phonegap.com to build the files instead of me building them.
I see this popping up a lot. But, none of the solutions out there helped me out. I have this code:
signin.js:
$(window).load(function() {
$('#login').submit(function(e){
e.preventDefault();
obj = $(this);
un = $("input[name='user']").val();
pass = $("input[name='pass']").val();
$.ajax({
url: https+"getUserDetails",
data: {
username: un,
password: pass
},
dataType: "jsonp",
crossDomain: true,
type: "POST",
jsonpCallback: "user",
success: function(response, textStatus, jqXHR){
if(response.exists){
alert(response.access_token);
window.localStorage.setItem("user", JSON.stringify({"name": response.name, "id": response.id, "token":response.access_token}, null));
runController();
}
else{
$("#er").show();
}
}
});
return false;
});
});
config.js:
addr = "domain.com"
api = "/api/v1/"
http = "http://"+addr+api
https = "https://"+addr+api
config.xml:
....
<access origin="*" subdomains="true" />
<feature name="http://api.phonegap.com/1.0/device" />
<feature name="http://api.phonegap.com/1.0/file" />
<feature name="http://api.phonegap.com/1.0/network" />
<preference name="phonegap-version" value="2.1.0" />
....
index.html:
<!DOCTYPE html>
<html>
<head>
....
<title>Sign In</title>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/controller.js"></script>
<script type="text/javascript" src="cordova-2.2.0.js"></script>
<script type="text/javascript" src="js/signin.js"></script>
....
</head>
<body>
....
<form id="login" action="#">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="input-block-level" placeholder="Email address" name="user">
<input type="password" class="input-block-level" placeholder="Password" name="pass">
<label class="checkbox">
<input type="checkbox" value="remember-me"> Remember me
</label>
<button class="btn btn-large btn-primary" type="submit">Sign in</button>
</form>
....
</body>
</html>
UPDATE 1
Here is what the server logs show:
Started POST "/api/v1/getUserDetails" for .... at 2012-11-29 20:38:22 -0800
Processing by Api::V1::ApiController#user_details as JSON
Parameters: {"username"=>"user", "password"=>"[FILTERED]"}
WARNING: Can't verify CSRF token authenticity
Completed 406 Not Acceptable in 15ms (ActiveRecord: 2.9ms)
UPDATE 2
I disabled CSRF tokens under my API controller since it is not needed and here is what my Server logs show when doing an ajax call under phonegap.
Started POST "/api/v1/getUserDetails" for .... at 2012-11-30 11:38:26 -0800
Processing by Api::V1::ApiController#user_details as JSON
Parameters: {"username"=>"user", "password"=>"[FILTERED]"}
Completed 406 Not Acceptable in 27ms (ActiveRecord: 0.4ms)
Still getting the 406 error. What is the next thing I should try?