我是新來的ajax和Django的Web開發。 現在我的模板包含:sample.html在Django問題中使用ajax代碼
<html>
<body>
<script language="javascript" type="text/javascript">
//Browser Support Code
function ajaxFunction(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
document.myForm.time.value = ajaxRequest.responseText;
}
}
ajaxRequest.open("GET", "/showtime/", true);
ajaxRequest.send(null);
}
</script>
<form name='myForm'>
Name: <input type='text' onBlur="ajaxFunction();" name='username' /> <br />
Time: <input type='text' name='time' />
</form>
</body>
</html>
在views.py我的功能是:
def showtime(request):
string = "Ajax Application"
data = {"string" : string}
pprint (data)
return render_to_response("sample.html",data)
現在,未如預期的輸出。該模板沒有收到服務器發送的迴應 代碼有什麼問題?
我在您的模板中的任何地方都看不到{{string}}。你的代碼是做什麼的? – 2010-06-07 11:37:41
模板收到了什麼?時間字段是否填充完整的html頁面? – Amarghosh 2010-06-07 11:52:17
你確定你有正確的URL模式條目嗎? – Amarghosh 2010-06-07 11:57:04