title: Properly get query using Javascript on Django
body: <p>I'm developing a website and one of my priorities is a dynamic search table with autocomplete etc.</p>
  <p>I found this possible using <strong>ajax</strong> and javascript.</p>
  <p>I already implemented  a trigger every time the user types in the search field.</p>
  <p>The current problem is getting data from the database, right now I'm getting a 404 error, and no data is being returned.</p>
  <p>Code:</p>
  <p>views.py</p>
  <pre><code>def search_bib(request):
      if request.method == &quot;POST&quot;:
          search_str=json.loads(request.body).get('searchText')
  
          bib = Bibrest51.objects.all().filter(
              autor__starts_with=search_str) | Bibrest51.objects.all().filter(
              autor__icontains=search_str)
  
          data = Bibrest51.objects.all()
          
          return jsonResponse(list(data), safe=False)
  </code></pre>
  <p>JS:</p>
  <pre><code>const searchField = document.querySelector(&quot;#searchField&quot;);
  
  searchField.addEventListener(&quot;keyup&quot;, (e) =&gt; {
    const searchValue = e.target.value;
  
    if (searchValue.trim().length &gt; 0) {
      console.log(&quot;searchValue&quot;, searchValue);
  
      fetch(&quot;bibliografia-search&quot;, {
        body: JSON.stringify({ searchText: searchValue }),
        method: &quot;POST&quot;,
      })
        .then((res) =&gt; console.log(res))
        .then((data) =&gt; {
          console.log(&quot;data&quot;, data);
        })
        .catch((e) =&gt; {
          console.log(e);
        });
    }
  });
  </code></pre>
  <p>Any help would be greatly appreciated, I'm new to the site and I hope I didn't do anything wrong with this post. Feedback for improving my post is also appreciated - thank you!</p>