感想
最近要做一个用户点击之后,后台做权限检查判断,判断完后,然后再做页面跳转的操作,然后我用的是javascript的ajax来做的,发现了不少的坑。
我的html代码为:
<div class="submitBox">
<button class="lead_btn" id='btn1'>问卷入口</button>
</div>
javascript代码为:
var btn1= document.getElementById("btn1");
var idValue = "{$info.id}";
// 提交的URL
var postUrl = "{: U('check', ['wpid' => WPID])}";
var target_url="{:U('singleSurvey','id='.$info[id])}";
btn1.onclick = function(){
$.ajax({
type: "POST",
url: postUrl,
data: {id:idValue},
success: function(data_return){
data=eval("("+data_return+")"); //这里解析json的时候有时候要用这种操作
// alert(ajaxobj['status']);
if(data.status == 1) {
/ alert(data.msg);
// window.close();
window.location.href =target_url;
// window.reload();
} else if(data.status == 0) {
alert(data.msg);
window.reload();
}
}
});
}
注意:
假设返回的json数据里有status及info2个属性 有时候可以直接ajaxobj.status或者ajaxobj["status"]去访问 但有时候,却要通过eval()或者 $.parsejson();才可以通过ajaxobj.status访问,而且这种情况下,需要是complete而不是success ajaxobj=eval("("+data+")");
详情,请见我的参考文献第二条
controller.class.php的代码为:
/**
* 在index页面检查,该用户所属院系是否合法
*/
function check(){
if(IS_POST) {
// 问卷id
$survey_id = I('post.id');
// jsonReturn(0, $survey_id, null);
$uid = get_uid_by_openid();
$this->checkDepartment($uid,$survey_id);
jsonReturn(1, '调研开始', null);
}
}
参考文献
[1].jQuery简单的Ajax调用示例.http://www.cnblogs.com/GarfieldTom/p/4277708.html
[2].Jquery ajax方法解析返回的json数据.https://www.cnblogs.com/fenglie/articles/5010254.html
[3].使用onclick跳转到其他页面/跳转到指定url.https://www.cnblogs.com/JuneZhang/archive/2010/11/25/1887575.html