用jquery+ajax,GET,然後用 eval() 算數
感覺很危險呢
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>計算機</title> | |
| <!-- Bootstrap4 --> | |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> | |
| <!-- JQuery --> | |
| <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> | |
| </head> | |
| <body> | |
| <div class="container-fluid"> | |
| <div class="row"> | |
| <div class="col-4"></div> | |
| <div class="col-4"> | |
| <h1> 輸入要運算的東東 </h1> | |
| <br> | |
| <input type="text" id="input" name="input"> | |
| <button id="click">計算</button> | |
| <br> | |
| <br> | |
| <h2 id="output"></h2> | |
| </div> | |
| <div class="col-4"></div> | |
| </div> | |
| </div> | |
| </body> | |
| <script> | |
| $("#click").click(function() { | |
| $.ajax({ | |
| url: "ajax?exp=" + encodeURIComponent($('#input').val()), | |
| success: function(result) { | |
| $("#output").html("答案:" + result); | |
| } | |
| }); | |
| }); | |
| </script> | |
| </html> |
| from django.conf.urls import url | |
| from . import views | |
| urlpatterns = [ | |
| url(r'^index$', views.index, name='index'), | |
| url(r'^ajax$',views.ajax,name='ajax') | |
| ] |
| from django.shortcuts import render | |
| from django.http import HttpResponse | |
| def index(request): | |
| context = {} | |
| return render(request, 'index.html', context) | |
| //get傳ajax | |
| def ajax(request): | |
| return HttpResponse(eval(request.GET['exp'])) |