如果JS会话变量为null,如何避免500内部服务器错误
How to avoid 500 Internal Server Error if a JS session variable null
我创建这个脚本是为了从各种web源读取。这个脚本更新所有通知(邮件、帖子、朋友)和他们自己的下拉窗口的详细信息,还一起更新两侧的更新栏。我在最上面的manu.php页面中使用了这个脚本。
此脚本适用于登录用户。
现在我用JavaScript var uid = '<? echo $session->id; ?>';
传递一个会话id变量,所以当用户没有登录时,我的浏览器控制台会显示500 Internal Server Error
,因为这里会话没有id,所以它传递uid=''
我该如何克服这个问题?
这是我的JavaScript脚本:
var uid = '<? echo $session->id; ?>';
function addmailno(type, msg){
//Do something for display mail/friend/post notification
}
function addmailup(type, msg){
//Do something for display mail/post/friend drop-down.
}
function addside(type, msg){
//Do something for display all friend/new post in side bar.
}
function waitFormailno(){
$.ajax({
type: "GET",
url: "serverforupandside.php",
cache: false,
async : false,
dataType : 'json',
data: "uid="+ uid,
timeout:15000,
success: function(data){
addmailno("MyDivClass", data);
addmailup("MyDivClass", data);
addside("MyDivId", data);
setTimeout(waitFormailno, 15000);
},
error: function(){
setTimeout(waitFormailno, 15000);
}
});
}
$(document).ready(function(){
waitFormailno();
});
服务器upandside.php
<?php
include("db.php");
include_once("mysession.php");
while (true) {
if($_GET['uid']){
global $dbh;
//All php query is here one after one
//Output is here by data
$data = array();
$data['upmail'] = $upmail;
$data['upfollow'] = $upfollow;
$data['uppost'] = $uppost;
// etc all
if (!empty($data)) {
echo json_encode($data);
flush();
mysqli_close($dbh);
}
}
mysqli_close($dbh);
}
?>
您的Javascript代码应修改为:
function waitFormailno(){
$.ajax({
type: "GET",
url: "serverforupandside.php",
cache: false,
async : false,
dataType : 'json',
data: "uid="+ uid,
timeout:15000,
success: function(data){
addmailno("MyDivClass", data);
addmailup("MyDivClass", data);
addside("MyDivId", data);
}
});
}
$(document).ready(function(){
setInterval(waitFormailno, 15000); // calls a function or evaluates an expression at specified intervals (in milliseconds)
});
PHP代码:
<?php
include("db.php");
include_once("mysession.php");
if (!empty($_GET['uid'])) {
global $dbh;
//All php query is here one after one
//Output is here by data
$data = array();
$data['upmail'] = $upmail;
$data['upfollow'] = $upfollow;
$data['uppost'] = $uppost;
// etc all
if (!empty($data)) {
echo json_encode($data);
}
}
此外,您应该在填充$data
数组之前添加验证。
相关文章:
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 如何将函数包装在函数中以避免代码重复
- 调用函数内部的函数
- 如何使用js将SNAPSHOT内部版本号转换为3位数的整数
- 在phonegap应用程序内部重定向不起作用
- 使用此选项选择父类内部的类
- 访问JSON对象内部的数组元素
- 从模块内部访问Express装载路径
- AngularJs指令,该指令创建内部有数据对象的新指令
- 如何使用内部对象构造对象
- 根据选项卡内部的文本链接中的哈希ID动态选择jqueryUI选项卡
- 用Javascript修改内部标记的CSS规则
- javascript函数内部的代码用逗号而不是分号分隔
- 如果JS会话变量为null,如何避免500内部服务器错误
- 可以避免在JavaScript中对isNaN()函数进行内部数字转换
- 如何避免重复记录为每个循环添加内部角度
- 避免在物体内部出现这种情况
- 避免nodeJs中的回调地狱/将变量传递给内部函数
- Javascript加密和安全:避免检查内部请求url
- 避免在单击内部时关闭下拉菜单