Marcolac
Marcolac

Reputation: 931

Post and get in a same jquery request

In my Django app I would like to be able to post and get in a same jQuery request.

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 'error' in the same jQuery request I used to post. Is it possible to do that?

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)

This way I first post the value username. If the username already exists result changes, and then I get it.

The trick was to put a post request inside a get request.

Upvotes: 4

Views: 169

Answers (4)

Doug
Doug

Reputation: 3312

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

$.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 this line (I would supply it but I'm not familiar with the ins and outs of Django):

error = 'Sorry this username is already taken'

That way your successful messages will be routed through the success handler and your errors will be routed through the error handler.

Upvotes: 2

Claudio Redi
Claudio Redi

Reputation: 68400

You could return an http code 400 (Bad Request) and add an error handler for you post request, that should do the trick.

I'm not very familiar with django so I'm just giving you the abstract idea.

Upvotes: 0

Alessandro Pezzato
Alessandro Pezzato

Reputation: 8802

If with get you mean the GET method, why not adding your query directly in the url parameter?

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

But maybe I'm misunderstooding the question...

Upvotes: 1

Antoine Pinsard
Antoine Pinsard

Reputation: 34942

I think you should look at the dajax API. This would do the job.

Upvotes: 0

Related Questions