Django为什么不使用`is_ajax()`将我的ajax请求视为ajax?

时间:2019-05-20 12:38:15

标签: jquery ajax django redirect request

我正在发送GET请求。我处理数据并获得重定向的链接。我了解需要将其发送回客户端并从中移出。链接有效。但是由于某些原因,该函数未进入is_ajax ()块。

$(document).on('click', '.top_up_pay', function (e) {
  var post_data;
  e.preventDefault();
  var amount = +$('tr.sum > td.price > span.value').text();
  var products_id = [];
  var products_count = [];
  var products_deliv = [];

  $.each($('.name'), function(){
    var id = $(this).attr('data-product_id');
    products_id.push(id);
    var count = $(this.parentNode.parentNode.parentNode).find('input[name="count"]');
    products_count.push(count.val());
    var deliv = $(this.parentNode.parentNode.parentNode).find('.delivery');
    products_deliv.push(deliv.val());
  })

  var new_post = $('.new_post > input')
  var ukr_post = $('.ukr_post > input')
  var recipient = $('.recipient > input')
  var phone = $('.phone > input')
  var sum = $('tr.sum > td.price > span.value');

  var data = {
    products_id: products_id,
    products_count: products_count,
    products_deliv: products_deliv,
    new_post: new_post.val(),
    ukr_post: ukr_post.val(),
    recipient: recipient.val(),
    phone: phone.val(),
    sum: sum.text(),
    comment: comment,
    amount: amount
  }

  $.ajax({
    type: "GET",
    url: "/payments/top-up-pay/",
    data: data,
    crossDomain: true,
    success: function(data){
      window.location = data.redirect_to
    },
    error: function(data){
      alert('Error')           //Error
    }
  })
  setTimeout(function () {
    window.location.href = "/products/";
  }, 2000);

观看次数

def top_up_pay(request):
    template = 'payments/top_up_pay.html'
    conf_currency = AppConfig.get_config('subscription_price_currency')    
    if request.method == "GET":
        global req_data
        req_data = get_request_data(request)
        amount = Decimal(req_data.get('amount'))
    if conf_currency:
        req_user = request.user
        invoice = Invoice.objects.create(
            invoice_type=INVOICE_TYPE.TOP_UP,
            user=req_user,
            amount=amount*100,
            currency=conf_currency
        )
        params = {
            "currency": invoice.currency,
            "amount": int(invoice.amount),
            'order_id': str(70),
            "operation_id": str(70),
            "order_desc": "payment",
        }
        integration = FondyIntegration(params)
        redirect_to = integration.checkout_url()
        if request.is_ajax():
            response = HttpResponse(json.dumps({'success': True, 'location': redirect_to}), content_type="application/json")
            return response
        else:
            return redirect(redirect_to)
    return render(request, template, ctx)

响应:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

我不会将其标记为重复,因为“为什么Django is_ajax()不能将我的请求视为AJAX请求?”与“为什么我的跨域ajax请求未设置X-Requested-With-Header”是一个完全不同的问题。

但这基本上就是原因,这就是答案:

Cross-Domain AJAX doesn't send X-Requested-With header

据我所知,由于您的AJAX请求是针对您自己的网站的,因此您无需进行此跨域的访问。成功响应后重定向到另一个域,但这不是AJAX调用本身。

答案 1 :(得分:0)

也许使用HttpResponseRedirect而不是在Javascript中呢?如果要发送Json,则使用JsonResponse吗?