哪位大哥能帮我写个原生的 JS getJSON,在此感谢了。不是异步请求,请求地址:get2.php?uuid=sssssss失败返回的JSON {"error_code": 0, "reason": "success", "resultcode": 200, "qr_code": null}点击按钮后执行一秒轮询一次get2.php?uuid=sssssss每轮询一次,按钮上的标题显示次数。轮询35秒后qr_code还是null,就让用户点击刷新页面。直到qr_code 栏里是URL 并执行跳转到该地址。
1 回答
慕莱坞森
TA贡献1810条经验 获得超4个赞
<input type="button" id="but" value="开始" onclick="run(1);" />
<script type="text/javascript">
function run(n) {
var xhr = new XMLHttpRequest();
xhr.open('get','get2.php?uuid=sssssss&time='+new Date().getTime());
xhr.onreadystatechange = function () {
if (xhr.readyState==4 &&xhr.status==200) {
document.getElementById("but").value = n;
var data = JSON.parse(xhr.responseText);
if (data.qr_code!=null) {
location.href = data.qr_code;
} else if (n<35) {
setTimeout(function(){
run(n+1);
}, 1000);
} else {
alert("刷新页面");
location.reload();
}
}
}
xhr.send();
}
</script>添加回答
举报
0/150
提交
取消
