from django.conf.urls import url from django.contrib import admin from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/',views.index), url(r'^get_json',views.get_json), url(r'^put_data',views.put_data), ]
from django.shortcuts import render,HttpResponse import json from queue import Queue QUE= Queue() def index(requset): return render(requset,'index.html') def get_json(request): # data = { # 'status':True, # 'msg':'xxxx' # } response = {'status':True,'data':None} try: data =QUE.get(timeout=10) response ['data'] = data except Exception as ex: response['status'] =False return HttpResponse(json.dumps(response)) def put_data(request): QUE.put('sa') return HttpResponse('扫码成功')
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Title</title> <script src="/static/bs/js/jquery-3.1.1.js"></script> </head> <body> <h1 id="i1">内容空</h1> <script> $(function () { //setInterval(func,2000); 轮询 func(); }); function func() { $.ajax({ url:'/get_json/', type:"POST", dataType:'JSON', success:function(arg){ if(arg.status){ location.href="https://www.baidu.com/" } else{ func(); } } }) } </script> </body> </html>