weixin_33711641 2011-11-19 05:56 采纳率: 0%
浏览 39

无法调用ajax / dajax方法

I am writing a django app where the user wants to click a button and have a partial page change. Data needs passed from the server to the web page without a needing a complete page refresh. That task sounded like a job for ajax. However, I can't make Ajax work in my app.

I cannot get the call into my server-side function. Below is the code the subject matter is regarding missed calls. My intent is to get the server side to return a list of missed calls and display it to the user without having to refresh the page.

When I click the button, I get a popup that says "Something goes wrong" using firebug, I traced this to a DAJAXICE_EXCEPTION but I don't know anything else about it.

What's going on here? How do I make this work? Also if there's an easier way to do this that doesn't require the Dajax library please advise. And any step-by-step examples would be very helpful.

Server side function

-------- /jim/ajax.py---------

@dajaxice_register 
def missedCalls(request, user): 
    print "Ajax:missedCalls"    #never prints...
    missedCalls = ScheduledCall.objects.filter(status__exact='Missed') 
    render = render_to_string('examples/pagination_page.html', { 'missedCalls': missedCalls }) 
    dajax = Dajax() 
    dajax.assign('#calls','innerHTML', render) 
    return dajax.json() 

-------page.html---------

 <script type='text/javascript'>
   function missed_calls_callback(data){
      # The dajax library wants a function as a return call.
      # Have no idea what I'm supposed to do with this part of the function.
      # what is supposed to go here?
      alert(data.message);
   }  
 </script>

 <!-- Button -->
 <input type="button" name="calltest" value="JQuery Test" 
    id="calltest" onclick="Dajaxice.jim.missedCalls(missed_calls_callback, {'user':{{ user }}})">


  <div id="calls">
     {% include "calls.html" %}
  </div>

--------calls.html--------

<h2> Missed Calls</h2>
<ul>         
{% for i in missedCalls.object_list %}         
    <li>{{ i }}</li>
{% endfor %}     
</ul>  
  • 写回答

1条回答 默认 最新

  • weixin_33725270 2011-11-19 09:41
    关注

    Before you start using a library, if might be helpful to do manually (to see what's going on).

    An ajax request is a HTTP request like any other except that it happens asynchronously (i.e. outside the normal request/response cycle) and it usually returns json or xml (although you can return html if you like).

    This means that to accept an AJAX request you just create an url and view as you would normally.

    urls.py

    ...
    url(r"^/my/ajax/path/$", myapp.views.ajax_view, name="do-something-ajaxy"),
    ...
    

    views.py

    def ajax_view(self, request):
        # Django's Request objects have a method is_ajax()* 
        # which checks the header to see if it's an 'ajax' request
        if request.is_ajax():
            raise Http404
        missedCalls = ScheduledCall.objects.filter(status__exact='Missed') 
        # You can return a dictionary-like json object which can be manipulated by the javascript when it receives it
        return HttpResponse(simplejson.dumps(missedCalls), mimetype='application/javascript')
    

    https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.is_ajax

    And using jquery to carry out the ajax request:

    (function($){
        $.ajax({
            type: 'GET',
            url: '/my/ajax/path/',
            success: function(data){
                for call in data:
                    /* Do something with each missed call */
            },
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型