我正在编写一个django应用程序,其中用户想要单击一个按钮,并有一个部分页面更改。数据需要从服务器传递到网页,而不需要完全刷新页面。这项任务听起来像是ajax的工作。但是,我不能让Ajax在我的应用程序中工作。
我不能让调用进入我的服务器端函数。下面是主题是关于未接来电的代码。我的目的是让服务器端返回未接来电的列表,并将其显示给用户,而无需刷新页面。
当我点击按钮时,我得到一个弹出窗口,上面写着使用firebug的“出了问题”,我追踪到了一个DAJAXICE_EXCEPTION,但我不知道其他任何关于它的信息。
这里发生了什么事?我该怎么做呢?此外,如果有一种更简单的方法,不需要Dajax库,请建议。任何一步一步的例子都会很有帮助。
服务器端函数
-/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> 发布于 2011-11-19 17:41:36
在你开始使用一个库之前,手动操作可能会有帮助(看看是怎么回事)。
ajax请求和其他请求一样,都是HTTP请求,只是它是异步发生的(即在正常的请求/响应周期之外),并且它通常返回json或xml (尽管如果您愿意,也可以返回html )。
这意味着要接受AJAX请求,您只需像往常一样创建一个url和视图。
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
并使用jquery执行ajax请求:
(function($){
$.ajax({
type: 'GET',
url: '/my/ajax/path/',
success: function(data){
for call in data:
/* Do something with each missed call */
},
});
});https://stackoverflow.com/questions/8192221
复制相似问题