Stuck with this silly problem for about 2 days already... this is my AJAX code (I use this for polling) :
Note that getParameterByName
is my own function, defined earlier on.
(function poll() {
setTimeout(function() {
$.ajax({
url: "../update.php",
method: "POST",
cache: false,
data: {"game": getParameterByName("game")},
dataType: "json",
complete: poll,
success: function(data) {
console.log("Updating");
console.log(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
}
});
}, 1000);
})();
This code is wrapped in a $(document).ready
statement, basically this sends a request to another page update.php
every second, if it succeeds, the data received is logged, if it fails, messages stating the errors are logged.
This is update.php
if it helps:
header('Content-type: application/json');
$game = $_POST["game"];
$returnArray = array();
// do stuff with the data......
echo json_encode($returnArray);
In the console when I run the code, this error is logged every second:
parsererror
SyntaxError: Unexpected end of input
at Object.parse (native)
at n.parseJSON (https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js:4:5309)
at uc (https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js:4:7333)
at x (https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js:4:10747)
at XMLHttpRequest.<anonymous> (https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js:4:14577)".
But somehow, when using incognito mode, it works completely fine.
I have no idea how to fix this, I searched online and tried many solutions like removing the "dataType" line but all of them don't work.
So how do I get my ajax code working (not just while using incognito mode?)
Thanks a lot.