HTTP Post提供200,但没有数据到达。异常处理

时间:2015-12-24 09:06:14

标签: angularjs api django-rest-framework

我可以在我的终端(本地测试)中看到“帖子”正在发生。但数据永远不会到来。在浏览器中,它告诉我一个200,但响应是我的异常错误(“不工作”)。

如何将数据实际发布到我的数据库?或者请建议一些错误/异常处理来尝试解决问题。

测试时,这很好用:

import requests
r = requests.post('http://localhost:8000/api/item/291/', {'uid':21, 'click':1, 'like':1, 'image':0, 'scroll':1, 'clickbuy':0})

Rest_views.py

from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from django.http import JsonResponse
import datetime
import json

def add_analytic(product, user, like, click, image, scroll, clickbuy):
    Analytic(product=product, user=user, like=like, click=click, image=image, scroll=scroll, clickbuy=clickbuy).save()

class ProductItemView(views.ObjectGetMixin, views.BaseObjectView):
    model = Product
    mapper_class = mappers.ProductMapper

    def post(self, request, *args, **kwargs):
        try:
            add_analytic(
                product=Product.objects.get(pk=kwargs['pk']), 
                user=User.objects.get(id=request.POST['uid']),
                like=int(request.POST['like']),
                click=int(request.POST['click']),
                image=int(request.POST['image']),
                scroll=int(request.POST['scroll']),
                clickbuy=int(request.POST['clickbuy']))
            return JsonResponse({'IT': 'WORKED'})
        except Exception:
            return JsonResponse({'Result': 'Not working'})

AngularJS:

.factory('cardsApi', ['$http', function ($http) {
        var like = JSON.stringify({'uid':21, 'click':1, 'like':1, 'image':0, 'scroll':1, 'clickbuy':0});

        var postRecordlikes = function (product_id) {
            $http({
                url: ('http://localhost:8000/item/' + product_id + "/"),
                dataType: 'json',
                method: 'POST',
                data: like,
                headers: {
                    "Content-Type": "application/json"
                }
        })
        }

        return {
            postRecordLikes: postRecordLikes,
        };
    }])

    .controller('CardsCtrl', ['$scope', 'TDCardDelegate', 'cardsApi', '$http',
        function ($scope, TDCardDelegate, cardsApi, $http) {
            console.log('CARDS CTRL');
            $scope.cards = []

            $scope.cardSwipedRight = function (card) {
                console.log('RIGHT');
                postRecordLikes(card);
            };


            var postRecordLikes = function (product_id) {
                cardsApi.postRecordLikes(product_id)
                    .then(function successCallback(product_id) {

                    }, function errorCallback(response) {
                        console.log(response);
                    });
            };

        }
    ])

Chrome网络检查员的结果:

enter image description here

1 个答案:

答案 0 :(得分:-1)

尝试使用request.data访问您的数据:

class ProductItemView(views.ObjectGetMixin, views.BaseObjectView):
    model = Product
    mapper_class = mappers.ProductMapper

    def post(self, request, *args, **kwargs):
        try:
            add_analytic(
                product=Product.objects.get(pk=kwargs['pk']), 
                user=User.objects.get(id=request.data['uid']),
                like=int(request.data['like']),
                click=int(request.data['click']),
                image=int(request.data['image']),
                scroll=int(request.data['scroll']),
                clickbuy=int(request.data['clickbuy']))
            return JsonResponse({'IT': 'WORKED'})
        except Exception:
            return JsonResponse({'Result': 'Not working'})