简体   繁体   English

在IOS中进行用户交互之前,Ajax调用不起作用

[英]Ajax call not working until user interaction in IOS

I am developing an application using phonegap and jQuery and I am facing a problem when performing ajax requests on iOS. 我正在使用phonegap和jQuery开发应用程序,并且在iOS上执行ajax请求时遇到问题。 I do the request , the php on my server receives the information and echo the correct answer. 我执行请求,服务器上的php接收信息并回显正确答案。 Turns out my app does not 'get' the information until I interact somehow with the screen (scrolling for example) or really wait too long ( over a minute ). 事实证明,直到我与屏幕进行某种交互(例如滚动)或真正等待太长时间(超过一分钟)后,我的应用程序才“获取”信息。 This problem evolved, in the beginning happened just after a few requests and now the first ajax already shows it . 这个问题发展了,一开始只是在几个请求之后发生,现在第一个ajax已经显示了它。 Another thing I noticed was, when taking out all ( or almost all ) javascript and/or css ( weird ) the problem disappears as if it was something with the phone memory . 我注意到的另一件事是,当取出所有(或几乎所有)javascript和/或css(奇怪)时,问题消失了,好像是手机内存中的东西一样。 When doing the request using async : false , the problem also disappears! 使用async : false进行请求时,问题也消失了! It happens on the iPhone 4 . 它发生在iPhone 4上。 Was tested on Android and PC (Chrome and Mozilla Firefox ) and it worked fine. 已在Android和PC(Chrome和Mozilla Firefox)上进行了测试,并且工作正常。

The weirdest thing is that when I interact with the screen , the answer appears, it do not wait a second, it is almost instantaneous ... as if the answer was already there, but not showing up for some reason. 最奇怪的是,当我与屏幕互动时,答案就会出现,它不用等待一秒钟,几乎是瞬间的……好像答案已经存在了,但由于某种原因没有出现。

Ps: The error alert don`t appears. 附:错误警告不出现。

jQuery.ajax({
  type: 'GET',
  url: 'url',
  crossDomain: true,
  data: {
    data: data
  },
  error: function() {
    alert('error');
  }
}).done(function(data) {
  alert(data);
});

http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/

if you are wanting a funciton to be triggered on response i would use the complete instead of done. 如果您想在响应时触发功能,我将使用complete而不是done。

My guess is you are not cancelling the click action and the page is refreshing 我的猜测是您没有取消点击操作,页面正在刷新

$(".yourSelector").on("click",function (evt) {
   evt.preventDefault();
   //your ajax call
});

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

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