联系我们:416326801#qq.com(#改成@)

Latest from the blog

ajax后台定时获取数据

标签: JavaScript Ajax

ajax后台获取json数据, 满足条件后刷新页面.

应用场景: A手机展示支付二维码, B手机扫码支付成功后, A手机每隔5秒从后台获取支付状态,成功即刷新页面.

<script type="text/javascript" src="/static/js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
//定时请求刷新
    setInterval(getStatus, 5000); //用户扫码付款, 后台定时获取status
    function getStatus() {
        var id = "<?php echo $id ?>";
        $.ajax({
            async: false,
            url: "qrcode.php", //PHP文件的地址
            type: "get", //get和post发送方式
            data: ({id: id, json: 1}),
            success: function (result) //返回json
            {
                if (result) {
                    var obj = eval("("+result+")");
                    if (obj.id==id&&obj.status==2) { //判断id一致, status已更新
                        location.reload(); //刷新页面
                    }
                }
            },
        });
    }
</script>

判断是否微信浏览器(微信客户端)

标签: PHP JavaScript HTML Weixin

PHP

function is_weixin() {
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        return true;
    }
    return false;
}


JavaScript

function is_weixin() {
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    } else {
        return false;
    }
}


HTML

<script type="text/javascript">
          var ua = navigator.userAgent.toLowerCase();
          var isWeixin = ua.indexOf('micromessenger') != -1;
          var isAndroid = ua.indexOf('android') != -1;
          var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);
          if (!isWeixin) {
              document.head.innerHTML = '<title>抱歉,出错了</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" >';
              document.body.innerHTML = '<div class="page_msg"><div class="inner"><span class="msg_icon_wrp"><i class="icon80_smile"></i></span><div class="msg_content"><h4>请在微信客户端打开链接</h4></div></div></div>';
          }
</script>