简体   繁体   English

发布并获取相同的jquery请求

[英]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. 在我的Django应用程序中,我希望能够发布并获得相同的jQuery请求。

My HTML: 我的HTML:

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

JS: 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. 我想在我过去发布的同一个jQuery请求中得到“错误”。 Is it possible to do that? 有可能吗?

Thank you for your help. 谢谢您的帮助。

EDIT: Here is the solution I found. 编辑:这是我找到的解决方案。

JS: 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: 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. 这样我首先发布值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. 诀窍是在get请求中放置一个post请求。

Instead of using the $.post, use the $.ajax command. 而不是使用$ .post,使用$ .ajax命令。 They do exactly the same things, but $.ajax gives you more features to play with. 它们完全相同,但$ .ajax为您提供了更多功能。

$.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): 然后添加一行将在此行上方返回403(或类似)HTTP错误代码(我会提供它,但我不熟悉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. 这样,您的成功消息将通过成功处理程序进行路由,您的错误将通过错误处理程序进行路由。

If with get you mean the GET method, why not adding your query directly in the url parameter? 如果用得到你的意思是GET方法,为什么不直接在网址参数相加查询?

  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... 但也许我误解了这个问题......

You could return an http code 400 (Bad Request) and add an error handler for you post request, that should do the trick. 您可以返回一个http代码400(错误请求)并为您post请求添加错误处理程序,这应该可以解决问题。

I'm not very familiar with django so I'm just giving you the abstract idea. 我对django不是很熟悉,所以我只是给你一个抽象的想法。

I think you should look at the dajax API . 我想你应该看看dajax API This would do the job. 这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM