'Unable to send the request to django views using xmlhttprequest

I am doing one application .In that i created the register function in Django views, and in html page, created form and trying to submit it with XMLHTTPRequest.But when i hit the submit button, total form data appending to url and not hitting the Django views register function.Below is the my function to submit the form.

function submitForm(oFormElement)
{
  // alert('called');
  var xhr = new XMLHttpRequest();
  xhr.onload = function(){ alert(xhr.responseText); }
  xhr.open("POST","/site/register/",true);
  xhr.setRequestHeader("X-CSRFToken", csrftoken); 
  alert('called');
  xhr.setRequestHeader('Authorization', 'Bearer ' + token); 
  xhr.send();
  alert('called')
  return false;
}

And if i mention the form details in send() also like below also same happening, total headers and data appending to url and not hit the Django Views method.

function submitForm(oFormElement)
{
  // alert('called');
  var xhr = new XMLHttpRequest();
  xhr.onload = function(){ alert(xhr.responseText); }
  xhr.open("POST","/site/register/",true);
  xhr.setRequestHeader("X-CSRFToken", csrftoken); 
  alert('called');
  xhr.setRequestHeader('Authorization', 'Bearer ' + token); 
  xhr.send(new FormData(oFormElement));
  alert('called')
  return false;
}

So please help me, how to hit the django views function when i submit the form from HTMl page.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source