Post and get the same jquery query

In my Django application, I would like to be able to send and receive the same jQuery query.

My HTML:

<form method="post" id='validation_form'> {% csrf_token %} <input id='username' type="text" name="username" value=""/> <button type="submit">Submit</button> </form> 

JS:

  var username = $('#username').val() $.post('/auth_validate', {'username': username}, function(data) { // Get Request to get something. } 

Views:

  def auth_validate(request): error = False if request.method == 'POST': username = request.POST.get('username') if User.objects.filter(username__exact=username).exists(): error = 'Sorry this username is already taken' 

I would like to get an β€œerror” in the same jQuery query that I used to publish. Is it possible to do this?

Thank you for your help.

EDIT: Here is the solution I found.

JS:

  function validateForm() { var username = $('#username').val() var result='' $.get('/auth_validate_username/', {'result':result}, function(data){ $.post('/auth_validate_username/', {'username': username}, function(data) { $('#error_message').html(data); }); }); return false; }; 

HTML:

  <form method="post" onsubmit="return validateForm();"> {% csrf_token %} <input id='username' type="text" name="username" value=""/> <div id='error_message'></div> <button type="submit">Submit</button> </form> 

Views:

  def auth_validate_username(request): result = '' if request.method == 'POST': username = request.POST.get('username') if User.objects.filter(username=username): result='Sorry but this name is already taken' return HttpResponse(result) 

Thus, I first send the username value. If the username already exists, the result changes, and then I get it.

The trick was to send a request for a request within a request for receipt.

+4
source share
4 answers

Instead of using $ .post, use the $ .ajax command. They do the same, but $ .ajax gives you more room to play.

 $.ajax({ type: 'POST', url: "/auth_validate", data: { 'username': username }, success: function(data){ //There was successfully data here! }, error: function(xhr, data, error){ //An error was thrown! } }); 

Then add a line that will return a 403 (or similar) HTTP error code just above that line (I would put it, but I am not familiar with Django's findings):

 error = 'Sorry this username is already taken' 

Thus, your successful messages will be redirected through the success handler, and your errors will be routed through the error handler.

+2
source

If with get you mean the GET method, why not add your request directly to the url parameter?

  var username = $('#username').val() $.post('/auth_validate?myvar=thevalue', {'username': username}, function(data) { // Get Request to get something. } 

But maybe I misunderstood the question ...

+1
source

You can return the 400 code (Bad Request) and add an error handler for the post request, which should do the trick.

I am not very familiar with django, so I am just giving you an abstract idea.

0
source

I think you should take a look at the dajax API . This will do the job.

0
source

All Articles