html5 ajax 请求 Django
code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml:lang="ko" lang="ko"> <head> <title>支付宝当面付</title> {% load static %} <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" /> <script src={% static '/js/qrcode.js'%}></script> </head> <body> <div align="center"> <h1>请使用支付宝扫码付款</h1> <p>订单编号: {{ out_trade_no }}</p> <div id="qrcode"></div> <button id='getXML' style="margin-top: 5em;">已支付,下一步</button> </div> <script type="text/javascript"> var qrcode = new QRCode('qrcode', { text: "{{ qrcode_url }}", width: 170, height: 170, colorDark : '#FF0000', colorLight : '#ffffff', correctLevel : QRCode.CorrectLevel.H }); /* 创建 XMLHttpRequest 对象 */ var xmlHttp; function GetXmlHttpObject(){ if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else{// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlhttp; } document.getElementById("getXML").addEventListener('click',function(){ xmlHttp=GetXmlHttpObject(); if (xmlHttp==null){ alert('您的浏览器不支持AJAX!'); return; } var url="{% url 'check_pay' out_trade_no %}"; xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange=function(){ if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){ console.log("trying"); } if (xmlHttp.readyState==4 && xmlHttp.status==200){ var d= xmlHttp.responseText; d=JSON.parse(d); console.log(d); if(d.is_paid){ console.log("支付成功!"); //window.location.href = "/pay_success"; }else{ alert("支付失败!"); } } };//发送事件后,收到信息了调用函数 xmlHttp.send(); }) </script> </body> </html>