【问题标题】:Undefined Queryset with Ajax使用 Ajax 的未定义查询集
【发布时间】:2016-04-13 23:39:53
【问题描述】:

这里是views.py

if request.is_ajax():
        minpricestore = list(StoreProduct.objects.filter(product__id=productobj).values_list('store__StoreName' , flat=True).annotate(Min('price')))
        print minpricestore
        minprice = list(StoreProduct.objects.filter(product__id=productobj).values_list('price' , flat=True).annotate(Min('price')).order_by('price'))
        print minprice
        minId =  list(StoreProduct.objects.filter(product__id=productobj).values_list('store__id' , flat=True).annotate(Min('price')).order_by('price'))
        print minId

        data = {
        'minpricestore' : minpricestore , 
        'minprice' : minprice , 
        'minId' : minId
        }

        JsonResponse(data)

这里是 ajax 脚本

<script type="text/javascript">
          function sendProduct(event, productId) {
        event.preventDefault();   
        var data = { productId : productId };
        // Send productId as query param of url
        $.ajax({        
            type: "GET",
            url: "{{instance.get_absolute_url}}",
            data: data,
            success: function(data) {
                alert(data.minprice)
            },
            error: function(response, error) {
                alert(error);  
            }
        });
    }
</script>

这里是查询集

minpricestore : [u'z store']
minprice: [1999]
minId:[1]

当我在 console.log 中显示未定义

ajax 工作正常,但无法显示查询集。我在使用警报时未定义。我该怎么做才能显示上述查询集?提前致谢

【问题讨论】:

  • 能不能做console.log(data)
  • console.log 返回一些 html
  • 什么是 html 请用那个 html 更新你的问题
  • 然后先解析数据再获取值。
  • 当我 console.log(data.minprice) 时显示未定义

标签: javascript jquery ajax django-templates


【解决方案1】:

添加在 Ajax 调用中打开的数据类型

dataType: "json",

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2017-02-09
    • 2023-03-12
    • 2021-06-29
    • 2015-03-13
    • 1970-01-01
    相关资源
    最近更新 更多