title: AJAX: POST data to a different url
body: <p>I'm creating an event registration page. </p>
  
  <p>On this page, users need to be able to search for a database item so it can be registered to this event. Therefore, the POST request must be submitted to a different URL than what the user is on, so that it doesn't interfere with the form wizard sequence. </p>
  
  <p>This is my javascript:</p>
  
  <pre><code>(document).ready(function() {
  
      $('#primary_artist_lookup').on('input', function(){
          console.log("Listener success");
          var searchItem = $('#primary_artist_lookup').val();
          $.ajax({
                  url : $('#lookupLink').val(),
                  type : "POST", //http method
                  data : searchItem,
                  dataType: "json",
  
                  // handle a successful response
                  success : function (json) {
                      console.log(json); // log the returned json to the console
                      console.log("success"); // another sanity check
                  },
                  // handle a non-successful response
                  error : function(xhr,errmsg,err) {
                      $('#results').html("&lt;div class='alert-box alert radius' data-alert&gt;Oops! We have encountered an error: "+errmsg+
                          " &lt;a href='#' class='close'&gt;&amp;times;&lt;/a&gt;&lt;/div&gt;"); // add the error to the dom
                      console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
                  }
              });
          });
  </code></pre>
  
  <p>The url is a hidden html value that maps to the url I am trying to POST the data to:</p>
  
  <p><code>&lt;p hidden id="lookupLink"&gt;{% url "Users:lookup" %}&lt;/p&gt;</code></p>
  
  <p>There is an additional js portion to submit a CSRF token to Django for security purposes:</p>
  
  <pre><code>// CSRF VALIDATION //
  
  // This function gets cookie with a given name
  function getCookie(name) {
      var cookieValue = null;
      if (document.cookie &amp;&amp; document.cookie != '') {
          var cookies = document.cookie.split(';');
          for (var i = 0; i &lt; cookies.length; i++) {
              var cookie = jQuery.trim(cookies[i]);
              // Does this cookie string begin with the name we want?
              if (cookie.substring(0, name.length + 1) == (name + '=')) {
                  cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                  break;
              }
          }
      }
      return cookieValue;
  }
  var csrftoken = getCookie('csrftoken');
  
  /*
  The functions below will create a header with csrftoken
  */
  
  function csrfSafeMethod(method) {
      // these HTTP methods do not require CSRF protection
      return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  }
  function sameOrigin(url) {
      // test that a given url is a same-origin URL
      // url could be relative or scheme relative or absolute
      var host = document.location.host; // host + port
      var protocol = document.location.protocol;
      var sr_origin = '//' + host;
      var origin = protocol + sr_origin;
      // Allow absolute or scheme relative URLs to same origin
      return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
          (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
          // or any other URL that isn't scheme relative or absolute i.e relative.
          !(/^(\/\/|http:|https:).*/.test(url));
  }
  
  $.ajaxSetup({
      beforeSend: function(xhr, settings) {
          if (!csrfSafeMethod(settings.type) &amp;&amp; sameOrigin(settings.url)) {
              // Send the token to same-origin, relative URLs only.
              // Send the token only if the method warrants CSRF protection
              // Using the CSRFToken value acquired earlier
              xhr.setRequestHeader("X-CSRFToken", csrftoken);
          }
      }
  });
  
  return false;
  
  });
  </code></pre>
  
  <p>When I try to use this ajax function, it doesn't post to the url I request, but rather the page that the users are on. What am I doing wrong?</p>