accessing list sent from server as JSON object

Posted by tazim on Stack Overflow See other posts from Stack Overflow or by tazim
Published on 2010-06-14T04:42:53Z Indexed on 2010/06/14 4:52 UTC
Read the original article Hit count: 243

Filed under:
|
|

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>  

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX