Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 空AJAX PUT请求体;Django Rest API_Javascript_Django_Ajax_Django Rest Framework - Fatal编程技术网

Javascript 空AJAX PUT请求体;Django Rest API

Javascript 空AJAX PUT请求体;Django Rest API,javascript,django,ajax,django-rest-framework,Javascript,Django,Ajax,Django Rest Framework,我在模仿StackOverflow对问题/答案的投票模板。单击向上投票/向下投票按钮后,将向API发送一个PUT请求。但是,DRF在相应的视图中找不到作为request.data=={}附加到请求正文的任何数据。我应该如何修复客户端API调用或API本身,以便有一个请求主体 vote_button.addEventListener("click", function(event) { voted_post = this.id.split("_")

我在模仿StackOverflow对问题/答案的投票模板。单击向上投票/向下投票按钮后,将向API发送一个
PUT
请求。但是,DRF在相应的视图中找不到作为
request.data=={}
附加到请求正文的任何数据。我应该如何修复客户端API调用或API本身,以便有一个请求主体

vote_button.addEventListener("click", function(event) {
    voted_post = this.id.split("_")
    const _question = voted_post[0];
    q_id = _question[_question.length - 1]
    vote = voted_post[-1]

    const headers = new Headers({
      "Accept": "application/json",
      "Content-Type": "application/json",
      "X-CSRFToken": csrftoken
    });

    const request = new Request(
      `http://localhost:8000/api/v1/questions/${q_id}/`, {
        "method": "put",
        "headers": headers,
        "body": JSON.stringify({
          "vote": vote
        })
      }
    );

    fetch(request).then((response) => {
      if (response.ok) {
        return response.json()
      }
    }).then((data) => {
      let vote_tally = document.getElementById(`question${q_id}_tally`);
      vote_tally.textContent = data['vote_tally']
    })
REST_FRAMEWORK = {
    'TEST_REQUEST_DEFAULT_FORMAT': 'json',
    'DEFAULT_PARSER_CLASSES': [
        'rest_framework.parsers.JSONParser'
    ],
    'DEFAULT_RENDERER_CLASSES': [
        'rest_framework.renderers.JSONRenderer'
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
    ],
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication'
    ],
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.ScopedRateThrottle'
    ],
    'DEFAULT_THROTTLE_RATES': {
        'voting': '5/minute'
    }
}
class UserQuestionVoteView(APIView):

    throttle_scope = "voting"

    def put(self, request, id):
        account = UserAccount.objects.get(user=request.user)
        question = get_object_or_404(Question, id=id)
        if account == question.user_account:
            return Response(data={
                'vote': "Cannot vote on your own question"
            }, status=400)
        try:
            stored_vote = QuestionVote.objects.get(
                account=account, question=question
            )
            serializer = QuestionVoteSerializer(stored_vote, request.data)
        except QuestionVote.DoesNotExist:
            serializer = QuestionVoteSerializer(data=request.data)
        finally:
            if serializer.is_valid(raise_exception=True):
                question_vote = serializer.save(
                    account=account,
                    question=question
                )
                vote = serializer.validated_data['vote']
                if vote == "downvote":
                    question.vote_tally = F('vote_tally') - 1
                else:
                    question.vote_tally = F('vote_tally') + 1
                question.save()
                question.refresh_from_db()
                return Response(data={
                    'id': question.id,
                    'tally': question.vote_tally
                })
            return Response(serializer.errors)