title: Django Javascript POST request
body: <p>On a Django website, I am trying to check a field input for database matches. I have search functions in my Django .py files, but since I can't run them after page load, I'm assuming a JavaScript POST request is the best option.</p>
  
  <pre><code>&lt;input id="this_field" onblur="querySomething()"&gt;
  </code></pre>
  
  <p>There are numerous SO questions on JS POST [<a href="https://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get">1</a>, <a href="https://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php">2</a>, <a href="https://stackoverflow.com/questions/13805340/post-is-not-working-anywhere-why">3</a>, etc], however, I haven't been able to get any of them to work for me. Here's one of my many attempts to get something useful going:</p>
  
  <pre><code>function querySomething(){
      $.ajax({
         type: "POST",
         url: "/my/url/",
         data: {"check_this": "123-456789",},
         success: function() {
           alert('success');
         }
      });
  </code></pre>
  
  <p>Or,</p>
  
  <pre><code>function postSearch() {
      $.post("/my/url/",
      {
          "check_this": "123-456789",
      },
      function(data, status){
          alert("Data: " + data + "\nStatus: " + status);
      })
  }
  </code></pre>
  
  <p>If I just treat the input like a search field and submit it (with JS or otherwise) it works fine. However, since I'm trying to provide results to help the user finish filling out the rest of the page, I need the results to be available on the same page as soon as the user focus leaves the field.</p>
  
  <p>I don't know if my <code>{% csrf_token %}</code> token comes into play here. I'm not getting any errors or messages in the console or in my server logs.</p>
  
  <p>// Edited version of code now reads:</p>
  
  <pre><code>function querySomething(){
      $.ajax({
          type: "POST",
          url: "/my/url/",
          headers: {
              "Content-Type": "application/json",
              "HTTP_GROUP_NAME": "groups_name",
          },
          data: {
              "check_this": $('#this_field').val(),
              "csrfmiddlewaretoken": "{{csrf_token}}",
          },
          success: function(data){
              console.log("success");
              console.log(data);
          },
          failure: function(data){
              console.log("failure");
              console.log(data);
          },
      });
      alert('marcusShep function ran.')
  }
  </code></pre>