2

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.

chris97ong
  • 6,870
  • 7
  • 32
  • 52
  • Developers tools->Network tab->xhr filter->Check the response you are retrieving from _update.php_ – Rayon Dec 12 '15 at 08:56
  • @RayonDabre Hmm... they say that "This request has no response data available", but in incognito mode (for Chrome) it has the response data I am expecting, any reason why that could be happening? – chris97ong Dec 12 '15 at 08:57
  • May be something related to `getParameterByName("game")` or your server side logic.. – Rayon Dec 12 '15 at 08:59
  • `getParameterByName` was a function from the answer of [this question](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript). What do you mean by server-side logic? @RayonDabre – chris97ong Dec 12 '15 at 09:01
  • You can check whether you are sending valid data in `data:{}`. I guess there must be some condition in your `php` logic and that condition is not being satisfied.. – Rayon Dec 12 '15 at 09:03
  • What do you mean by "valid data"? Just tested out in the console, `{"game": getParameterByName("game")}` is `{game: 1}`. If there's a problem somewhere why would it still work in incognito mode? @RayonDabre – chris97ong Dec 12 '15 at 09:08
  • Replace your _update.php_ with ` true); echo json_encode($returnArray);` and try..Its working as expected..If it works for you too then let me know.. – Rayon Dec 13 '15 at 11:24

0 Answers0