我有Django
个应用,我试图在index2.html
事件index.html
上打开button click
。基本上,我使用2个程序来完成这个简单的任务
第一个似乎工作正常,但Ajax
无效。点击后,Button 2
似乎没有任何事情发生。任何人都可以告诉我发生了什么,任何帮助都会受到赞赏。
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Index</title>
</head>
<body>
<h2>Index</h2>
<form method="post" action="/index2/">
<button type="submit" >Button 1</button>
<input type="button" id="btn" name="btn" value="Button 2" />
</form>
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script>
$("#btn").click(function() {
$.ajax({
type: 'POST',
url: '/index2/',
data: {}
});
});
</script>
</body>
</html>
index2.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Index 2</title>
</head>
<body>
<h2>Index 2</h2>
</body>
</html>
views.py
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render
@csrf_exempt
def index(request):
return render(request, "index.html")
@csrf_exempt
def index2(request):
return render(request, "index2.html")
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('index/', views.index, name='index'),
path('index2/', views.index2, name='index2'),
]