2016-03-10 201 views
-1

的index.html:Django的AJAX不起作用

<head> 

{% load staticfiles %} 
<script src={% static "/static/jquery.min.js" %}></script> 

<script> 
function loadDoc() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
     if (xhttp.readyState == 4 && xhttp.status == 200) { 
      document.getElementById("demo").innerHTML = xhttp.responseText; 
     } 
    }; 
xhttp.open("GET", "text.html", true); 
xhttp.send(); 
} 
</script> 

</head> 

<p id="ajax">hello wordl</p> 
<button type="button" onclick="loadDoc()">Change Content</button> 

和text.html:

<p>goodbye</p> 

這兩個文件都在同一個目錄(模板目錄)。 單擊按鈕沒有任何反應。

回答

0

我找到了答案。我說:

def text(request): 
    return render(request, 'text.html') 

到views.py,這:

url(r'^text', text), 

到urls.py.