使用ajax Get方法从视图中检索数据

时间:2019-05-29 06:27:20

标签: jquery ajax get django-templates django-views

我想使用ajax get方法从Django视图中检索一些数据。

这是我想要数据的模板(/figures.html)中的jquery脚本:

<script>
$.ajax({
   url : "{% url 'livedb_model:typeDetail' property.id roomtype.id %}",
   type: 'GET',
   dataType:'json',
   success : function (data) {
           alert(data)
       }
   });
$.getJSON("{% url 'livedb_model:typeDetail' property.id roomtype.id %}",function(result){
   alert(result)
})
</script>

这是视图的一部分,其中包含我要检索的数据:

def change_figures(request,id,rt_id):
    property=get_object_or_404(Property,id=id)
    roomtype=get_object_or_404(RoomType,id=rt_id)
    x = {
        "name": "John",
        "age": 30,
        "city": "New York"
        }
    y = json.dumps(x)
    return render(request,'livedb_model/typeDetail.html',{
        'property':property,
        'y':y,
        'roomtype':roomtype,
        })

我认为我可以使用ajax get方法在视图中仅检索y变量,因为它是json中唯一的变量,但我不能。 您是否知道我可以将ajax方法更改为在警报中仅显示y的方式?

谢谢您的帮助。

0 个答案:

没有答案
相关问题