如何在Django应用中实现AJAX?
我的问题跟这里提到的很相似:
我尝试了里面给出的解决方案,但还是没有得到响应值。以下是我的代码:
chkAjax.html
------------
<html>
<head>
<script>
function chkAjax(form)
{
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert(xmlhttp.responseText);
document.getElementById("t1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","/sign?text="+form.text1.value,true);
xmlhttp.send();
}
function DoNav(theUrl,form)
{
theUrl = theUrl + '?text=' + form.text1.value;
document.location.href = theUrl;
}
</script>
</head>
<body>
<form method="get" action="">
<input type="text" name="text1" id="t1" onkeyup = 'chkAjax(this.form)'/>
</form>
</body>
</html>
下面是我的处理程序的代码:
from django.http import HttpResponse
c=self.request.get('text')
c=c + "b"
self.response.write("hi")
通过使用alert()我发现我确实收到了响应,但值是空的,所以请帮帮我。