使用ajax显示queryset时未定义

时间:2022-10-17 09:15:14

here is views.py

这是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)

here is the ajax script

这是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>

here is the queryset

这是查询集

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

when i console.log it says undefined

当我在console.log中说它未定义

ajax is working fine but unable to display the queryset.I get undefined when using alert.what can i do to to display the above queryset? Thanks in Advance

ajax工作正常,但无法显示queryset。我在使用alert时未定义。我可以做什么来显示上面的查询集?提前致谢

1 个解决方案

#1


0  

Add the dataType open in Ajax call

在Ajax调用中添加打开的dataType

dataType: "json",

#1


0  

Add the dataType open in Ajax call

在Ajax调用中添加打开的dataType

dataType: "json",