角度http和表单发送GET而不是POST

时间:2016-08-09 13:12:50

标签: angularjs

当runnig这个angularjs代码调用一个rest API端点时,我得到方法不允许错误。我尝试了一个正常的形式但没有成功。

休息api课程

class LoginView(APIView):
#serializer_class = UserSerializer

def post(self, request):
    data = json.loads(request.data.decode('utf-8'))
    print("request data", data)
    print("request body", request.body)
    print("request post",request.POST)
    username = request.data.get('email', None)
    password = request.data.get('password', None)

    user = authenticate(username=username, password=password)

    if user is not None:
        if user.is_active:
            login(request, user)

            serialized = UserSerializer(user)

            return Response(serialized.data)
        else:
            return Response({
                'status': 'Unauthorized',
                'message': 'This account has been disabled.'
            }, status=status.HTTP_401_UNAUTHORIZED)
    else:
        return Response({
            'status': 'Unauthorized',
            'message': 'Username/password combination invalid.'
        }, status=status.HTTP_401_UNAUTHORIZED)

angularjs代码

app.controller('LoginController',['$scope','$http','$cookies','$location', function($scope,$http,$cookies,$location){
//$http.defaults.headers.post['X-CSRFToken'] = $cookies['csrftoken'];
var self = this;
$scope.loginModel = {};
$scope.onlog = function(event){
console.log($scope.loginModel);
console.log("enviando processo ...!");

dado = angular.toJson($scope.loginModel);
console.log(dado);

 var req = {
    url: '/api/v1/auth/login/',
    method: 'POST',
    dataType: 'json',
    data: {hahaha:"ahahah",haha:"auauau"},
    headers: { 'Content-Type': 'application/json; charset=UTF-8','X-CSRFToken': $cookies['csrftoken'] }
 }

$http(req).then( function(response){
    console.log(":)");
    console.log(angular.toJson(response));
 },function(response){
    console.log(":(");
    console.log(response)
    })
};
}]);

我做错了什么?请帮忙,这是一个学校项目

0 个答案:

没有答案