Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t调用ajax/dajax方法_Javascript_Ajax_Django - Fatal编程技术网

Javascript Can';t调用ajax/dajax方法

Javascript Can';t调用ajax/dajax方法,javascript,ajax,django,Javascript,Ajax,Django,我正在编写一个django应用程序,其中用户希望单击一个按钮并进行部分页面更改。数据需要从服务器传递到web页面,而不需要完全刷新页面。这项任务听起来像是ajax的工作。然而,我无法使Ajax在我的应用程序中工作 我无法将调用放入服务器端函数。以下是关于未接来电的主题代码。我的目的是让服务器端返回未接来电列表,并将其显示给用户,而无需刷新页面 当我点击按钮时,我得到一个弹出窗口,上面用firebug写着“出了问题”,我追踪到了一个DAJAXICE_异常,但我不知道关于它的任何其他信息 这是怎么回

我正在编写一个django应用程序,其中用户希望单击一个按钮并进行部分页面更改。数据需要从服务器传递到web页面,而不需要完全刷新页面。这项任务听起来像是ajax的工作。然而,我无法使Ajax在我的应用程序中工作

我无法将调用放入服务器端函数。以下是关于未接来电的主题代码。我的目的是让服务器端返回未接来电列表,并将其显示给用户,而无需刷新页面

当我点击按钮时,我得到一个弹出窗口,上面用firebug写着“出了问题”,我追踪到了一个DAJAXICE_异常,但我不知道关于它的任何其他信息

这是怎么回事?我该怎么做?另外,如果有一种更简单的方法不需要Dajax库,请提供建议。任何一步一步的例子都会很有帮助

服务器端功能 --------/jim/ajax.py---------

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


函数未命中\u调用\u回调(数据){
#dajax库需要一个函数作为返回调用。
#我不知道我该怎么处理这部分函数。
#这里应该放什么?
警报(数据、消息);
}  
{%include“calls.html”%}
--------calls.html--------

未接来电
    {missedCalls.object_list%}
  • {{i}
  • {%endfor%}

在开始使用库之前,手动执行if可能会有所帮助(以查看发生了什么)

ajax请求与其他任何请求一样,都是一个HTTP请求,但它是异步发生的(即,在正常的请求/响应周期之外),并且通常返回json或xml(尽管您可以根据需要返回html)

这意味着,要接受AJAX请求,只需像平常一样创建url和视图

url.py

...
url(r"^/my/ajax/path/$", myapp.views.ajax_view, name="do-something-ajaxy"),
...
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')
视图.py

...
url(r"^/my/ajax/path/$", myapp.views.ajax_view, name="do-something-ajaxy"),
...
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')

并使用jquery执行ajax请求:

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

什么是
(函数($)…
,它去了哪里?我的所有其他函数都在
块中,看起来像
函数(数据){something}
它是用于执行ajax调用的javascript函数,即当您单击按钮或其他东西时。它也在标记中,因为它是javascript。我把它写成了一个匿名函数,这意味着一旦遇到它,它就会运行,但是你可以用你熟悉的方式编写它,除非你需要单独调用它
(function($){
    $.ajax({
        type: 'GET',
        url: '/my/ajax/path/',
        success: function(data){
            for call in data:
                /* Do something with each missed call */
        },
    });
});