【问题标题】:Dajax - function in ajax.py not workingDajax - ajax.py 中的函数不起作用
【发布时间】:2014-01-01 23:38:51
【问题描述】:

所以我正在尝试为 Django 掌握 Dajax 的窍门。 一切都很好,直到我在ajax.py 中创建了第二个函数。

我创建了一个新项目,其中包含一个example 应用程序。因此,我在模板中制作了一个按钮 - Button 1,它使用了ajax.py 中的函数,效果很好。 Button 2 不起作用,它使用了ajax.py 中的第二个函数。我在下面粘贴了index.html 和ajax.py 代码。我怎样才能让Button 2 工作,并让它做我想做的事情。

index.html

{% load dajaxice_templatetags %}
{% dajaxice_js_import %}

<input type="button" value="Button 1" onclick="Dajaxice.example.sayhello(my_js_callback);"/>
<br>
<input type="text" id="text"/>
<input type="button" value="Button 2" onclick="Dajaxice.example.saytext(my_js_callback, {'text':$('#text').val()});"/>

<script type="text/javascript">
    function my_js_callback(data){
    alert(data.message);
}
</script>

ajax.py

from django.utils import simplejson
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def sayhello(request):
    return simplejson.dumps({'message':'Hello World!'})

@dajaxice_register
def saytext(request, text):
    return simplejson.dumps({'message':'%s' % text})

【问题讨论】:

    标签: django dajax


    【解决方案1】:

    所以,在花了几个小时在网上搜索之后,我终于在某个地方读到“如果你只使用 Python 2.7,它会让你免于很多痛苦”。这就是我所做的,我卸载了 Python 3.3 并安装了 2.7,它工作正常!

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 2015-01-23
      • 2012-12-06
      • 2014-01-12
      • 2012-08-01
      • 2017-12-01
      • 2013-12-03
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多