简体   繁体   English

Dajax示例不起作用

[英]Dajax example doesnt work

So I'm trying to get the hang of Dajaxice for Django. 因此,我试图摆脱Dajaxice对Django的影响。 Everything was fine till I used Dajaxice, but just I tried Dajax I gat trouble. 在我使用Dajaxice之前,一切都很好,但是我尝试Dajax时遇到了麻烦。

I made a new project and inside it an example app. 我创建了一个新项目,并在其中包含一个示例应用程序。 So then I made a button - Button 1 in a template which uses a function in ajax.py, this worked fine. 因此,然后我制作了一个按钮-模板中的按钮1,该模板使用ajax.py中的函数,此方法工作正常。 Button 2 did not work though, which uses the second function in ajax.py. 但是,按钮2无效,它使用了ajax.py中的第二个功能。 I have pasted the index.html and ajax.py code below. 我在下面粘贴了index.html和ajax.py代码。 How can I get the Button 2 to work, and make it do what I want it to do. 我如何才能使Button 2正常工作,并使它按我想要的方式工作。

index.html 的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 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})

this has been said multiple times over the last few years. 在过去的几年中,这已经被多次提及。 The Dajaxice project is a bad idea and you should just use JQuery and AJAX instead to post/receive data to your django view. Dajaxice项目不是一个好主意,您应该只使用JQuery和AJAX将数据发布/接收到django视图。

The author has stated on his Github page; 作者在他的Github页面上说过; "These days using this project is a bad idea." “如今使用这个项目是一个坏主意。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM