Stack Exchange
AJAX call to Django View returns None
I trying to get data which I send with AJAX

base.html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
  <script>

    function getAjaxResponse() {
      console.log('myAjax');
      $.getJSON('/ajax', { get_param: 'data' }, function(data) {
        $.each(data, function(key, val) {
            console.log(key);
            console.log(val); 
        });
      });
    }

    $(function(){
      $("input[name='barcode']").change(function() {  
        var barcode = document.getElementById("id_barcode").value;
        $.ajax({
          type:'GET',
          url:`ajax/?variable=${barcode}`,
          headers: {'X-CSRFToken': '{{ csrf_token }}'},
          success: function(response){
              console.log('success');
              console.log(barcode);
              setTimeout(getAjaxResponse, 3000);
          },
          error: function (response){
              console.log('error');
          }
        });              
      });     
    });            

  </script>


urls.py

path('ajax/', views.ajaxBarcode, name='ajaxBarcode'), 


views.py

def ajaxBarcode(request):
    barcode = request.GET.get('variable')
    dict = {}
    if len(str(barcode)):
        dict['hint1'] = str(barcode)
    return JsonResponse(dict)

When I retrieve data in browser I always had hint1 and None.
But when I type in address line /ajax/?variable=115 I have response {"hint1": "115"}

len(str(barcode)) is 4 and "truthy" when barcode is None -> "None" into the dict. Use if barcode is not None. You need to debug your JS to check why it is not passed.
