Question

I have a .getJSON() function which fires on button click (random record from DB). It works fine for desktop, but on mobile devices works just one time. Then it keeps delivering the same record. I guess it even doesn't do a AJAX call to php because random.php file creates a JSON file every time php is requested.. and on mobile requests the JSON file is not created..

jQuery part:

    $("#random_post").click(function(){
        $.getJSON("random.php",function(result){
          $.each(result, function(i, field){
            $("#selected-name").text("Name: ".concat(field.name));
            $("#selected-link").text("Link: ".concat(field.link));
            $("#selected-date").text("Date: ".concat(field.date));
            $("#selected-content").text("Content: ".concat(field.content));
            $("#selected-video").text("Video: ".concat(field.video));             
          });
        });

    });

PHP delivery part is usual:

echo json_encode($data);
$fp = fopen('results2.json', 'w');
fwrite($fp, json_encode($data));
fclose($fp);

The button is Random post at http://raigle.com/a.php

Was it helpful?

Solution

It's probably caching, use $.ajax instead (docs here) $.getJSON is just a wrapper (and in my option not a very good one) for this function anyway.

$.ajax({cache:false,
    url:"random.php",
    dataType :'json'})
    .done(function(result){
      .....etc
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top