简单的ajax调用在django中不起作用
Simple ajax call doesnt work in django
首先,我花时间阅读和理解javascript和ajax,但即使我的代码应该工作时,检查它对其他类型的,似乎是完全相同的,这仍然不工作。我没有做任何花式在这里,只是返回一个简单的文本文件名为信息。文本
我用的是django,下面是我创建的简单视图:
def ajax(request):
return render(request, "base/info.text")
这是简单的ajax/javascript代码:
1 var window, document, submitButton;
2
3 submitButton = document.getElementsByName("form")[0];
4 submitButton.addEventListener("submit", fireAjaxRequest, false);
5
6 function handleResponse() {
7 if (httpRequest.readyState === 4) {
8 alert("response received...");
9 try {
10 alert("in try catch")
11 if (httpRequest.status === 200) {
12 alert(httpRequest.responseText);
13 } else {
14 alert("Request failed, status code received from the server is: " + httpRequest.statusText);
15 }
16 }
17 catch (exception) {
18 alert("exception: " + exception.description);
19 }
20 }
21 }
22
23 function fireAjaxRequest() {
24 console.log("firing the ajax request...");
25 httpRequest = new XMLHttpRequest();
26
27 if (! httpRequest) {
28 alert("not able to create XMLHttpRequest");
29 } else {
30 console.log("created XMLHttpRequest");
31 }
32
33 httpRequest.onreadystatechange = handleResponse;
34 httpRequest.open("GET", "javascript/ajax", true);
35 httpRequest.send(null);
36 console.log("request sent, waiting for response...");
37 }
我知道console.log更好,但是它只是在我点击提交按钮时立即清除console.log。
在open()调用中,除了"/javascript/ajax"之外,我还尝试了"http://domain.com:8000/javascript/ajax",这也不起作用。
html代码为:
3 <html>
4 <head>
5 <title>Javascript testing</title>
6 </head>
7 <body>
8 Welcome to my site
9 <br><br>
11 <br><br>
12 <form name="form">
13 First name: <input type="text" id="firstName"></input>
14 <br>
15 Last name: <input type="text" id="lastName"></input>
16 <br>
17 <input type="submit" id="submit" value="Send registration">
18 <br>
19 </form>
20 </body>
21 <script type="text/javascript" src="{% static 'js/js_test.js' %}"></script>
22 </html>
url .py只是指向上面的视图(^javascript/ajax)。
我不明白为什么这个简单的请求不起作用,它没有任何意义。
请求的输出。由于某些原因,状态为0
您可能需要捕获并阻止表单上的默认事件。
在你的javascript中试试:
function fireAjaxRequest(e) {
// Insert this
e.preventDefault();
24 console.log("firing the ajax request...");
25 httpRequest = new XMLHttpRequest();
26
27 if (! httpRequest) {
28 alert("not able to create XMLHttpRequest");
29 } else {
30 console.log("created XMLHttpRequest");
31 }
32
33 httpRequest.onreadystatechange = handleResponse;
34 httpRequest.open("GET", "javascript/ajax", true);
35 httpRequest.send(null);
36 console.log("request sent, waiting for response...");
37 }
相关文章:
- jquery在Django中不起作用
- Django 模板内联 jQuery 不起作用
- Int Array Django 通过上下文传递到 JS 不起作用
- 系统配置默认扩展对 Django 不起作用
- Django jQuery不起作用
- AngularJS ng-repeat在Django驱动的网页上不起作用
- 静态文件被接收,但在 Django 中不起作用
- Django-smart-selects在模板中不起作用
- 欧芹.js在我的 Django 表单验证中不起作用
- Django {{ block.super }} 在特定情况下不起作用
- 简单的 ajax GET 请求在我的 Django 网页上不起作用
- 使用Ajax的Javascript POST到Django不起作用
- Django和Javascript:依赖下拉列表不起作用
- jQuery在django中不起作用
- Django动态表单集不起作用
- 作为JS的字符串传递,django不起作用
- Django-将JSON数据和HTML一起传递到前端不起作用
- 简单的ajax调用在django中不起作用
- & # 39;安全# 39;Filter在textarea - django中不起作用
- 在实际的项目环境(Django)中,鼠标在后台移动的示例不起作用