Я пытаюсь понять, как работают вызовы Django + Jquery и Ajax.
Это просто / test / url, который показывает одну форму ввода, после отправки получает ответ с сервера через ajax.
Для этого я написал очень маленькое представление:
def test(request):
if request.is_ajax():
from django.http import HttpResponse
post_text = request.POST.get("post_data")
return HttpResponse("{'response_text': '"+post_text+" recieved.'}", mimetype="application/json")
else:
return render_to_response('test.html', {},context_instance =RequestContext(request))
И я написал это правило для URL в свой urlpattern по адресу urls.py:
.
(r'^test/$', 'myapp.views.test'),
А вот мой шаблон test.html:
<html>
<head><title>template</title></head>
<script type="text/javascript" src="/media/js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#post_form').submit(function(event){
event.preventDefault(); // cancel the default action
var form = this;
var data = {}
data.post_data = $(form).find('input[@name=our_text]').val();
$.post("/test/",
data,
function(responseData) {
alert(responseData.response_text);
},
"json"
);
});
});
</script>
<body>
<form id="post_form" method="post">
INPUT: <input type="text" name="our_text" />
<input type="submit" value="Add" />
</form>
</body>
</html>
Похоже, он не отвечает на мой www.mysite.com/test/, когда я заполняю поле ввода и отправляю. В чем может быть проблема?