Skip to content
Advertisement

syntax error only on first load, after refresh page then will be gone

I got this error:

“Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0”

whenever I clear browsing data and refresh page to load my API, but it only shows in the first load, then will be gone after refresh again. looks it beacuse of cookies or somethings, is there anybody can help me out? Thanks for any help!

JS CODE:

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 CODE:

 if(!isset($_SESSION['sessionOBJ'])) {//check session
        $_SESSION['sessionOBJ'] = new doctorSession(); //instantiate session to start using
    }

Advertisement

Answer

at last I found I forget to these two lines in the begining of my php file, private $_startTime; private $_requestCounter; (I used them in my php file for ratelimit)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement