简体   繁体   English

Ajax帖子通话不适用于移动设备

[英]Ajax post call not working on mobile

New to rails and ajax. Rails和Ajax的新手。

I created a little web app where the user can simply count their steps, pressing a button. 我创建了一个小型Web应用程序,用户可以通过按一个按钮简单地计算其步数。 Once completed, the user click submit and the number of steps are being saved. 完成后,用户单击提交,并保存步骤数。

This all works great on my pc. 这一切在我的电脑上都很好用。 However, when I try this on mobile, the number of steps are not being saved. 但是,当我在移动设备上尝试此操作时,没有保存步骤数。 And I can't figure out why this is. 我不知道为什么会这样。

My code looks like this: 我的代码如下所示:

<script type="text/javascript">
$(document).ready(function(){

var index = 0;

function resetcounter() {
  index = 0;
  document.getElementById("button1").value = index;
}

function count(){
  index += 1;
  document.getElementById('button1').value=index;
}

$('#button1').click(function() {
    count();
});

$('#resetButton').click(function() {
    resetcounter();
});

$('#submitButton').click(function(e){
  e.preventDefault();
  e.stopImmediatePropagation();
    $.ajax({
      url : "/steps",
      type: "POST",
      data: {
        step: {
          stepcount: index
        }
      },
      success: function( data, textStatus, jQxhr ){ $('#response pre').html( data ); }, 
      error: function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown ); }
    });
    window.location = '/steps';
  });

});

<div class="panel panel-default center">
  <div class="panel-heading">
    <h2>Count your steps</h2>
  </div>
  <div class="panel-body">
<input type="button" class="btn btn-default btn-circle" id="button1" value="0"/>
  </div>
  <div class="panel-footer">
    <button type="button" id="submitButton" class="btn btn-default ">Submit</button>
    <button type="button" id="resetButton" class="btn btn-default">Reset</button>

  </div>
</div>

The problem with your code is that you're redirecting the user before the AJAX request has a chance to complete (resulting in unstable behavior). 代码的问题在于,您要在AJAX请求有机会完成之前重定向用户(导致行为不稳定)。

If you are dead set on using your current implementation, you can fix this by moving the window.location redirect to inside the success callback so it only redirects once the AJAX response comes back successfully. 如果您不愿意使用当前的实现,则可以通过将window.location重定向移动到成功回调内部来解决此问题,以便仅在AJAX响应成功返回后才进行重定向。

$('#submitButton').click(function (e) {
    e.preventDefault();
    e.stopImmediatePropagation();
    $.ajax({
        url: "/steps",
        type: "POST",
        data: {
            step: {
                stepcount: index
            }
        },
        success: function (data, textStatus, jQxhr) {
            $('#response pre').html(data);
            window.location = '/steps';
        },
        error: function (jqXhr, textStatus, errorThrown) {
            console.log(errorThrown);
        }
    });
});

The reason why you're seeing this behavior on mobile is because the AJAX request takes longer to perform over a slow connection and therefore has less of a chance to get dispatched successfully before your script redirects itself. 之所以会在移动设备上看到此行为,是因为AJAX请求需要较长的时间才能通过慢速连接执行,因此在脚本重定向自身之前,成功调度的可能性较小。

However, as I mentioned in the comments, what you're trying to achieve does not need AJAX. 然而,正如我在评论中提到的,你想实现并不需要AJAX的东西。 You can synchronously POST an HTML form with the same data and the user will not know the difference. 您可以同步发布具有相同数据的HTML表单,而用户将不知道区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM