Javascript 仅在第一次加载时出现语法错误,刷新后页面将消失
我得到了这个错误: “未捕获(承诺中)语法错误:JSON中位置0处的意外标记<” 每当我清除浏览数据并刷新页面以加载我的API时,它只在第一次加载时显示,然后在再次刷新后消失。 看来是因为饼干什么的,有人能帮我吗?谢谢你的帮助 JS代码:Javascript 仅在第一次加载时出现语法错误,刷新后页面将消失,javascript,api,fetch,Javascript,Api,Fetch,我得到了这个错误: “未捕获(承诺中)语法错误:JSON中位置0处的意外标记
function showDoctorinfo(){
loading();
var url = './api/api.php?action=showDoctorinfo';
fetch(url,
{
method: 'GET',
credentials: 'include',
headers: {
// 'Content-Type': 'application/json',
// 'Accept': 'application/json'
}
},
)
.then(function(response) {
if(response.status === 204) {
closeLoading();
document.getElementById("output-searchDoctorInfo").style.display = 'none';
document.getElementById("allDoctors").style.display = 'none';
console.log('no doctor information')
return;
}
if(response.status === 429) {
closeLoading();
document.getElementById("output-searchDoctorInfo").style.display = 'none';
document.getElementById("allDoctors").style.display = 'none';
console.log('rate limit exceeded')
return;
}
response.json().then(function(data){
closeLoading();
document.getElementById("output-searchDoctorInfo").style.display = 'none';
document.getElementById("allDoctors").style.display = 'none';
document.getElementById("output-doctorinfo").style.display = 'block';
console.log(data);
var source = document.getElementById("doctorinfo-template").innerHTML;
var myTpl = Handlebars.compile(source);
Handlebars.registerHelper("compare",function(v1, v2, options){
if(v1 > v2){
return options.fn(this);
}else{
return options.inverse(this);
}
})
var compiledData = myTpl(data);
document.getElementById("output-doctorinfo").innerHTML = compiledData;
})
})
.catch(function () {
closeLoading();
document.getElementById("alertMessage").style.display = "block";
document.getElementById("alertMessage").innerHTML="Unalbe to connect, please check your internet connection and try again..";
setTimeout(function(){ document.getElementById("alertMessage").style.display="none"}, 4000);
});
}
outStr = showDoctorinfo();
API.PHP代码:
if(!isset($_SESSION['sessionOBJ'])) {//check session
$_SESSION['sessionOBJ'] = new doctorSession(); //instantiate session to start using
}
最后,我发现我忘记了php文件private$\u startTime开头的这两行代码;私人$u请求计数器;(我在用于ratelimit的php文件中使用了它们)很难说,因为您的php代码不够完整;毫无疑问,当会话已经存在时,您的PHP代码或服务器会弹出一些HTML错误页面——整个API响应创建不都在那个负面的“检查会话”块中吗?您在网络控制台中看到的确切响应主体是什么?你的应用程序是否安装了服务人员?这是否回答了你的问题?React Js:Uncaught(承诺中)语法错误:JSON中位置0处的意外标记<