Read Ajax post data in django












0















I got an Ajax request using promise in my django project:



var path = window.location.pathname;
fetch('/getblogs/', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({'path': path})
}).then(function (response) {
return response.json();
});


The request is in a js file and there is no form.



I'm trying to read data in my views.py like this:



@csrf_exempt
def get_blogs(request):
cat_id = request.POST.get('path')
print("RESULT: " + str(cat_id))


But in output I get:



RESULT: None


Am I missing somthing in reading post data or there is something wrong with my ajax request?










share|improve this question



























    0















    I got an Ajax request using promise in my django project:



    var path = window.location.pathname;
    fetch('/getblogs/', {
    method: 'post',
    headers: {
    'Accept': 'application/json, text/plain, */*',
    'Content-Type': 'application/json'
    },
    body: JSON.stringify({'path': path})
    }).then(function (response) {
    return response.json();
    });


    The request is in a js file and there is no form.



    I'm trying to read data in my views.py like this:



    @csrf_exempt
    def get_blogs(request):
    cat_id = request.POST.get('path')
    print("RESULT: " + str(cat_id))


    But in output I get:



    RESULT: None


    Am I missing somthing in reading post data or there is something wrong with my ajax request?










    share|improve this question

























      0












      0








      0








      I got an Ajax request using promise in my django project:



      var path = window.location.pathname;
      fetch('/getblogs/', {
      method: 'post',
      headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json'
      },
      body: JSON.stringify({'path': path})
      }).then(function (response) {
      return response.json();
      });


      The request is in a js file and there is no form.



      I'm trying to read data in my views.py like this:



      @csrf_exempt
      def get_blogs(request):
      cat_id = request.POST.get('path')
      print("RESULT: " + str(cat_id))


      But in output I get:



      RESULT: None


      Am I missing somthing in reading post data or there is something wrong with my ajax request?










      share|improve this question














      I got an Ajax request using promise in my django project:



      var path = window.location.pathname;
      fetch('/getblogs/', {
      method: 'post',
      headers: {
      'Accept': 'application/json, text/plain, */*',
      'Content-Type': 'application/json'
      },
      body: JSON.stringify({'path': path})
      }).then(function (response) {
      return response.json();
      });


      The request is in a js file and there is no form.



      I'm trying to read data in my views.py like this:



      @csrf_exempt
      def get_blogs(request):
      cat_id = request.POST.get('path')
      print("RESULT: " + str(cat_id))


      But in output I get:



      RESULT: None


      Am I missing somthing in reading post data or there is something wrong with my ajax request?







      python ajax django es6-promise






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 8:30









      Alex JoligAlex Jolig

      8,3511674110




      8,3511674110
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I think you can try like this:



          import json

          @csrf_exempt
          def get_blogs(request):
          cat_id = json.loads(request.body).get('path')
          print("RESULT: " + str(cat_id))





          share|improve this answer



















          • 1





            I should've gone for body not post. This solved the issue. Thanks!

            – Alex Jolig
            Nov 19 '18 at 8:43



















          1














          From the Django documentation




          HttpRequest.POST



          A dictionary-like object containing all given HTTP POST parameters,
          providing that the request contains form data. See the QueryDict
          documentation below. If you need to access raw or non-form data posted
          in the request, access this through the HttpRequest.body attribute
          instead.




          Try using json.loads(request.body)['path']






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370872%2fread-ajax-post-data-in-django%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I think you can try like this:



            import json

            @csrf_exempt
            def get_blogs(request):
            cat_id = json.loads(request.body).get('path')
            print("RESULT: " + str(cat_id))





            share|improve this answer



















            • 1





              I should've gone for body not post. This solved the issue. Thanks!

              – Alex Jolig
              Nov 19 '18 at 8:43
















            1














            I think you can try like this:



            import json

            @csrf_exempt
            def get_blogs(request):
            cat_id = json.loads(request.body).get('path')
            print("RESULT: " + str(cat_id))





            share|improve this answer



















            • 1





              I should've gone for body not post. This solved the issue. Thanks!

              – Alex Jolig
              Nov 19 '18 at 8:43














            1












            1








            1







            I think you can try like this:



            import json

            @csrf_exempt
            def get_blogs(request):
            cat_id = json.loads(request.body).get('path')
            print("RESULT: " + str(cat_id))





            share|improve this answer













            I think you can try like this:



            import json

            @csrf_exempt
            def get_blogs(request):
            cat_id = json.loads(request.body).get('path')
            print("RESULT: " + str(cat_id))






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 8:35









            ruddraruddra

            12.5k32648




            12.5k32648








            • 1





              I should've gone for body not post. This solved the issue. Thanks!

              – Alex Jolig
              Nov 19 '18 at 8:43














            • 1





              I should've gone for body not post. This solved the issue. Thanks!

              – Alex Jolig
              Nov 19 '18 at 8:43








            1




            1





            I should've gone for body not post. This solved the issue. Thanks!

            – Alex Jolig
            Nov 19 '18 at 8:43





            I should've gone for body not post. This solved the issue. Thanks!

            – Alex Jolig
            Nov 19 '18 at 8:43













            1














            From the Django documentation




            HttpRequest.POST



            A dictionary-like object containing all given HTTP POST parameters,
            providing that the request contains form data. See the QueryDict
            documentation below. If you need to access raw or non-form data posted
            in the request, access this through the HttpRequest.body attribute
            instead.




            Try using json.loads(request.body)['path']






            share|improve this answer




























              1














              From the Django documentation




              HttpRequest.POST



              A dictionary-like object containing all given HTTP POST parameters,
              providing that the request contains form data. See the QueryDict
              documentation below. If you need to access raw or non-form data posted
              in the request, access this through the HttpRequest.body attribute
              instead.




              Try using json.loads(request.body)['path']






              share|improve this answer


























                1












                1








                1







                From the Django documentation




                HttpRequest.POST



                A dictionary-like object containing all given HTTP POST parameters,
                providing that the request contains form data. See the QueryDict
                documentation below. If you need to access raw or non-form data posted
                in the request, access this through the HttpRequest.body attribute
                instead.




                Try using json.loads(request.body)['path']






                share|improve this answer













                From the Django documentation




                HttpRequest.POST



                A dictionary-like object containing all given HTTP POST parameters,
                providing that the request contains form data. See the QueryDict
                documentation below. If you need to access raw or non-form data posted
                in the request, access this through the HttpRequest.body attribute
                instead.




                Try using json.loads(request.body)['path']







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 8:36









                nightgauntnightgaunt

                11310




                11310






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370872%2fread-ajax-post-data-in-django%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents