【问题标题】:syntax error only on first load, after refresh page then will be gone仅在第一次加载时出现语法错误,刷新页面后就会消失
【发布时间】:2021-02-02 07:29:21
【问题描述】:

我收到了这个错误:

"Uncaught (in promise) SyntaxError: Unexpected token

每当我清除浏览数据并刷新页面以加载我的 API,但它只在第一次加载时显示,然后再次刷新后将消失。 看起来是因为饼干之类的,有人可以帮我吗?感谢您的帮助!

JS代码:

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 代码不够完整;疯狂的猜测是,当会话已经存在时,您的 PHP 代码或服务器会吐出一些 HTML 错误页面——不是在那个否定的“检查会话”块中创建整个 API 响应吗?您在网络控制台中看到的确切响应正文是什么?您的应用是否安装了任何 Service Worker?

标签: javascript api fetch


【解决方案1】:

最后我发现我忘记了我的php文件开头的这两行,private $_startTime;私人 $_requestCounter; (我在我的 php 文件中使用它们来限制速率)

【讨论】:

    猜你喜欢
    • 2011-10-22
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多