Javascript 使用Ajax的未定义查询集
这里是views.pyJavascript 使用Ajax的未定义查询集,javascript,jquery,ajax,django-templates,Javascript,Jquery,Ajax,Django Templates,这里是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.ob
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>
当我输入console.log时,它表示未定义
ajax工作正常,但无法显示查询集。我在使用alert时未定义。如何显示上述查询集?提前感谢添加在Ajax调用中打开的数据类型
dataType: "json",
你能做console.log(data)console.log返回一些htmlWhat是html请用该HTMLH更新你的问题然后在得到值之前先解析数据。当i console.log(data.minprice)时,它表示未定义
dataType: "json",