【问题标题】:.getJSON works just one time on mobiles.getJSON 在手机上只运行一次
【发布时间】:2014-05-08 23:45:07
【问题描述】:

我有一个 .getJSON() 函数,它在按钮单击时触发(来自 DB 的随机记录)。它适用于桌面设备,但在移动设备上只能使用一次。然后它继续提供相同的记录。我猜它甚至不会对 php 进行 AJAX 调用,因为每次请求 php 时,random.php 文件都会创建一个 JSON 文件.. 并且在移动请求上,不会创建 JSON 文件..

jQuery 部分:

    $("#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交付部分是通常的:

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

按钮是http://raigle.com/a.php随机帖子

【问题讨论】:

  • @Pinch 我应该在哪里以及如何做到这一点?
  • 只是一个建议,你的$("#random_post").click应该return false
  • @jeremy 我玩过,没有效果

标签: jquery getjson


【解决方案1】:

它可能是缓存,使用 $.ajax 代替 (docs here) $.getJSON 只是这个函数的一个包装器(在我看来不是一个很好的包装器)。

$.ajax({cache:false,
    url:"random.php",
    dataType :'json'})
    .done(function(result){
      .....etc
    });

【讨论】:

    猜你喜欢
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    相关资源
    最近更新 更多