【问题标题】:Page redirection after Ajax request in javascriptjavascript中的Ajax请求后页面重定向
【发布时间】:2014-10-23 06:40:08
【问题描述】:

我用 jquery 发送一个 ajax 发布请求:

$(function(){

    $(".palcesubm").click(function(){
        var location = $('#detail_address').val()
        $.post('/wechat/locate/select/current/',{'location':location})
    })

});

在我看来,我可以收到这个请求。然后我保存数据,并使用 HttpResponseRedirect 将请求重定向到另一个 url

@csrf_exempt
def confirm_location(request):
    if request.method == 'GET':
        lat = request.session.get('latitude')
        lng = request.session.get('longitude')
        area, detail_address = get_area_and_detail_address(lat, lng)

        t= loader.get_template('confirm_location.html')
        html = t.render(Context({'area':area, 'detail_address': detail_address}))
        return HttpResponse(html)

    else:
        request.session['location'] = request.POST.get('location')
        redirect_url = '/wechat/stores/direct/list?first_level='+ str(request.session.get('first_level'))

        print redirect_url
        return HttpResponseRedirect(redirect_url)

这是重定向网址的视图

def direct_store_list(request):

    first_level = request.GET.get('first_level')
    request.session['first_level'] = first_level
    store_level_list = get_store_level_list(first_level)
    merchant_list = []
    location =''

    lat = request.session.get('latitude')
    lng = request.session.get('longitude')
    if request.session.get('telphone') is None:
        if request.session.get('location') is None:
            location = get_store_location(lat,lng)
        else:
            location = request.session.get('location')
            del request.session['location']
    else:
        if request.session.get('location') is None:
            location = get_user_default_location(request.session.get('telphone'))
        else:
            location = request.session.get('location')
            del request.session['location']

    merchant_list = get_merchant_list(first_level,lat,lng)

    t= loader.get_template('direct_store_list.html')
    html = t.render(Context({'store_level_list':store_level_list, 'merchant_list':merchant_list,
                             'location':location}))

    return HttpResponse(html)

在终端中,http 响应 200 OK,但页面不会像正常的 HttpResponseRedirect 那样重定向到该页面。如何解决这个问题呢?谢谢

[23/Oct/2014 14:34:23] "POST /wechat/locate/select/current/ HTTP/1.1" 302 0
[23/Oct/2014 14:34:24] "GET /wechat/stores/direct/list?first_level=1 HTTP/1.1" 200 2452

【问题讨论】:

    标签: jquery django


    【解决方案1】:

    如果您使用 AJAX ($.post) 发送请求,您将重定向什么?您可以发送要重定向的 URL 并在 $.post 响应处理中执行 window.location = redirectionURL 的解决方案。

    【讨论】:

      【解决方案2】:

      您的 JS 中没有任何代码来处理 Ajax 发布的结果,因此不会发生任何事情。

      但是,如果您想重定向,我完全不明白您为什么要使用 Ajax:只需执行正常的 POST。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多