【发布时间】:2013-04-06 12:55:32
【问题描述】:
包含一些数据的json文件。使用jQuery我需要显示到页面中。
.json 文件是
{
"p1":{
"item1":"apple",
"item2":"orange",
"item3":"banana",
},
"p2":{
"item1":"water",
"item2":"milk",
"item3":"alcohol",
}
}
我的模板是
<html>
<head>
<body>
<ul></ul>
<button></button>
<script src="script/jquery-1.9.1,min.js" type="text/javascript"></script>
<script src="script/myscript.js" type="text/javascript"></script>
</body>
</head>
</html>
myscript.js 文件是
$("button").click(function(){
$.getJSON("item.json",function(obj){
$.each(obj,function(key,value){
$("ul").append("<li>+value.item1+"</li>");
$("ul").append("<li>+value.item2+"</li>");
$("ul").append("<li>+value.item3+"</li>");
});
});
});
当按下页面中的“getjson”按钮时,页面中加载了 Json 数据,但页面正在刷新,没有加载任何数据。如何做到这一点,我认为上面的代码中有一些错误,或者我是否需要编写任何函数在views.py中使用python。
请帮我做这件事。
谢谢
【问题讨论】:
-
您遇到什么错误?您需要将 django 服务器的 url 传递给
$.getJSON()函数。 -
我什么也没得到,只是按下按钮时页面会刷新
-
item.json文件存储在哪里?它是否由您的 django 服务器提供服务? -
按钮点击必须是
$("#button").click(function(){,忘了#,还要放正确的json文件路径 -
item.json 在我的应用文件夹中,位置是 /fixtures/item.json
标签: jquery python django json django-views