使用Ajax的未定义Queryset

Undefined Queryset with Ajax

本文关键字:Queryset 未定义 Ajax 使用      更新时间:2023-09-26

这里是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工作正常,但无法显示查询集。我在使用alert时未定义。我可以做些什么来显示上面的查询集?提前感谢

添加在Ajax调用中打开的dataType

dataType: "json",