Django-ajax多个请求。


                    

我正在使用django-ajax模块,并且有一些基于ajax的工作:</ p>

views.py </ strong> </ p>

  @ajax
@要求登录
def search_dist(request):
     dist_list = Distributor.objects.all()
     如果request.method =='GET':
         starts_with = request.GET ['query']

     如果starts_with:
         dist_list = Distributor.objects.filter(
             姓__istartswith = starts_with)
     返回渲染(请求,“ distributors / distributors_list.html”,{“ distributors”:dist_list})
</ code> </ pre>

JS </ strong> </ p>

  var search = $('#search');
search.on('keyup',function(){
     window.setTimeout(function(){
         var查询;
         查询= search.val();
         ajaxGet('{%url“ distributors:search_dist”%}',{
             '查询':查询
         },函数(内容){
             $('#distributors')。html(content);
             set_favorite();
         })
     },1000);
});
</ code> </ pre>

它可以正常工作,但是当我打开控制台时,我看到服务器对同一事物的请求过多。 例如:输入“ Jhon”后,我看到
[03 / Jun / 2015 10:56:36]“ GET / distributors / search_dist?query = HTTP / 1.1” 200 </ code>
25次。 我的代码有问题吗?还是很正常?</ p>
     </ div>

展开原文

原文

I am working with django-ajax module and I have some ajax based work:

views.py

@ajax
@login_required
def search_dist(request):
    dist_list = Distributor.objects.all()
    if request.method == 'GET':
        starts_with = request.GET['query']

    if starts_with:
        dist_list = Distributor.objects.filter(
            surname__istartswith=starts_with)
    return render(request, 'distributors/distributors_list.html', {'distributors': dist_list})

JS

var search = $('#search');
search.on('keyup', function() {
    window.setTimeout(function() {
        var query;
        query = search.val();
        ajaxGet('{% url "distributors:search_dist" %}', {
            'query': query
        }, function(content) {
            $('#distributors').html(content);
            set_favorite();
        })
    }, 1000);
});

It works just fine, but when I open my console I see that server makes too many requests for the same thing. For example: after I input 'Jhon' I see [03/Jun/2015 10:56:36]"GET /distributors/search_dist?query= HTTP/1.1" 200 25 times. Is something wrong with my code or it's just normal?

1个回答


我看到您只使用了 setTimeout </ code>。 但是您也需要使用 clearTimeout </ code>函数。</ p>

  search.on('keyup',function(){
     var timer;
     clearTimeout(timer);
     var query = search.val();
     计时器= setTimeout(function(){
         ajaxGet('{%url“ distributors:search_dist”%}',{
             '查询':查询
         },函数(内容){
             $('#distributors')。html(content);
             set_favorite();
         })
     },1000);
});
</ code> </ pre>
     </ div>

展开原文

原文

I see you've only used setTimeout. But you'll need to use clearTimeout function, too.

search.on('keyup', function() {
    var timer;
    clearTimeout(timer);
    var query = search.val();
    timer = setTimeout(function() {
        ajaxGet('{% url "distributors:search_dist" %}', {
            'query': query
        }, function(content) {
            $('#distributors').html(content);
            set_favorite();
        })
    }, 1000);
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐