出于某种原因, $.ajax() 没有被调用。它正在检测 $("#login").submit() 函数,因为我在那里确实有警报并且它调用了它。
我正在使用http://build.phonegap.com来构建文件而不是我构建它们。
我看到这个弹出很多。但是,那里的解决方案都没有帮助我。我有这个代码:
登录.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;
});
});
配置.js:
addr = "domain.com"
api = "/api/v1/"
http = "http://"+addr+api
https = "https://"+addr+api
配置.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" />
....
索引.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>
更新 1
以下是服务器日志显示的内容:
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)
更新 2
我在我的 API 控制器下禁用了 CSRF 令牌,因为它不需要,这是我的服务器日志在 phonegap 下进行 ajax 调用时显示的内容。
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)
仍然收到 406 错误。接下来我应该尝试什么?