【问题标题】:Trouble getting field data from AJAX call从 AJAX 调用获取字段数据时遇到问题
【发布时间】:2023-04-10 13:55:01
【问题描述】:

我想要做的是,当从下拉列表中选择金融机构时,我想进行 AJAX 调用以将所选的 FI 与我的查询进行比较。

当我尝试打印出选定的数据时,我每次都会得到None

这是我目前所拥有的。

edit.html javascript:

q = $("#investment_form").serialize();

function cdic_limit() {
        $.ajax({
        type: "POST",
        url: "{% url investments-calc_cdic plan.id %}",
        dataType: "json",
        data: q,
        success: function(json) {
            alert('made it here');
            }
        });
    }

在我的 ajax.py 中:

def calc_cdic(request, plan, *args, **kwargs):
    from investments.models import Investment
    from financial_institutions.models import FinancialInstitution
    from profiles.models import Profile
    from plans.models import Plan
    json = {}
    data = request.POST

    if request.is_ajax():
        print 'THIS IS THE PLAN: ', plan
        fiid = data.get('id_financial_institution')
        print 'THIS IS THE SELECTED FI:  ', fiid
        json['fiid'] = fiid

    return HttpResponse(simplejson.dumps(json), mimetype='application/json')

id_financial_institution 是我试图从中提取数据的表单上的字段名称。

任何帮助将不胜感激。

【问题讨论】:

  • 在您的 Javascript 代码中有一个变量 q。这个变量的值是多少?顺便说一句,我会将 $.ajax() 替换为 $.post()。
  • Ajax 函数中q 来自哪里?
  • data:q 是什么?那个变量是从哪里来的?
  • 抱歉 q 是一个复制和粘贴错误...用于从旧函数添加到查询字符串
  • 如果可能(即,除非它是敏感的),请始终发布所有相关代码。不可避免地,您可能认为不重要的部分是其他人为了诊断您的问题而需要查看的部分。

标签: python ajax django django-views


【解决方案1】:

我猜你得到的响应是 403(禁止),你需要在你的视图中添加一个@csrf_exempt (1)。

【讨论】:

    猜你喜欢
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2021-03-03
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多