accessing list sent from server as JSON object
- by tazim
How to access a list sent in form of json object using django to the template received in ajax callback function .
The code is as follows :
views.py
def showfiledata(request):
with open("/home/tazim/webexample/test.txt") as f:
list = f.readlines()
f.closed
return_dict = {'filedata':list}
json = simplejson.dumps(return_dict)
HttpResponse(json,mimetype="application/json")
in template showfile.html:
< html>
< head>
< script type="text/javascript" src="/jquerycall/">< /script>
< script type="text/javascript">
$(document).ready(function()
{
$("button").click(function()
{
$.ajax({
type:"POST",
url:"/showfiledata/",
datatype:"json",
success:function(data)
{
var s = data.filedata;
$("#someid").html(s);
}
});
});
});
< /script>
< /head>
< body>
< form method="post">
< button type="button">Click Me< /button>
< div id="someid">< /div>
< /form>
< /body>
< /html>