我正在使用 Tastypie 制作我的 API。我正在使用以下功能登录
def signin(self, request, **kwargs):
self.method_check(request, allowed=['post'])
data = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
username = data.get('username', '')
password = data.get('password', '')
try:
user = User.objects.get(username=data.get('username', ''))
if user.get_profile().status == 3 or user.get_profile().status == 4:
return self.create_response(request, {'success': False, 'reason': 'Account Disabled or Closed' })
user = authenticate(username=username, password=password)
if user:
if user.get_profile().status == 2:
return self.create_response(request, {'success': True,'is_verified':False })
return self.create_response(request, {'success': True,'is_verified':True })
else:
return self.create_response(request, {'success': False, 'reason': 'Wrong Email or Password' })
except User.DoesNotExist:
return self.create_response(request, { 'success': False, 'reason':'Email not registered' })
但是当我使用 jQuery 登录时
<script type="text/javascript">
$(document).ready(function() {
$( "#signin" ).click(function() {
var username=$("#inputEmail1").val();
var password=$("#inputPassword1").val();
if (username.length == 0 || password.length == 0) {
alert('enter email and password to login.')
}else{
$.ajax({
type: "POST",
url: "/api/v1/user/signin/",
data: JSON.stringify({ 'username':username,'password':password }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data, status, xhr){
$.cookie();
if (data.success){
if (data.is_verified) {
alert('Logged in and is_verified');
}else{
alert('Logged in but not verified');
}
}else{
alert('Incorrect username or password');
}
},
failure: function(errMsg) {
alert(errMsg);
}
});
};
});
})
</script>
但是当我尝试使用 $.cookie(); 获取我的 cookie 时 在 firebug 中(使用jquery-cookie),但它返回一个空对象(Object {})
我错过了什么吗?我应该添加一些东西吗?这确实适用于 iOS。它以某种方式获取 cookie 并记住会话。但不是在这里。