skip to Main Content

I try to submit data on change and change the response from the view based on the data from the POST request.

      $.ajax({
        url: $basicAnalysisChart.data("url"),
        type: "POST",
        headers:{
        "X-CSRFToken": csrftoken
    },
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data:{
            selector1:$("#Select1").val(),
            selector2:$("#Select2").val()
        },

i tried getting to that data in my view with request.POST.get("Select1", "") and request.POST.get("selector1", "") but cant get it working.

2

Answers


  1. Chosen as BEST ANSWER

    i didnt format the json object right, so it couldnt work. Porblem solved


  2. As your data type is JSON, in django you should say

    data = request.body
    
    

    instead of:

    request.POST.get("Select1", "")
    

    From the documentation: "If you need to access raw or non-form data posted in the request, access this through the HttpRequest.body attribute instead."

    here is the link to documentation: https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpRequest.POST

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search