jQuery.ajax重新加载页面而不是在iOS上的Safari中执行ajax请求

bsc*_*ter 6 ajax jquery ios

我在本地apache实例(/test)上的页面上调用了一个函数,该函数调用子页面(/test/info)jQuery.ajax并正确地进行AJAX调用,并在FF,Safari,Chrome中的桌面上动态加载响应中的内容,但是iOS模拟器,不进行任何调用并刷新页面.

window.getInfo = function ( event ) {
  console.log('1) prior to $.ajax');
  $.ajax({
    url: 'http://localhost/test/info',
    dataType: 'html',
    beforeSend: function(xhr) { 
      console.log('2) beforeSend'); 
    },
    success: function(data, textStatus) {
      console.log('3) success');
      if ( textStatus == 'success' ) {
        // doing stuff with data
      }
    }
  }).always( function() { console.log('4) always'); });
};
Run Code Online (Sandbox Code Playgroud)

从桌面浏览器的所有日志的打印和我的Apache服务器报告的要求/test,但在Safari浏览器中的iOS模拟器,只有'1) prior to $.ajax''2) beforeSend'日志打印和阿帕奇提出下一个请求是/test.

有没有人知道这里发生了什么,以及如何使iOS表现自己?

更新:当我将async: false属性添加到ajax调用时,打印所有日志,并发出请求,这样基本上解决了问题; 然而,页面仍然重新加载,我认为这是与iOS上的事件传播相关的不同问题.

bsc*_*ter 5

完成此工作所需的全部是return false;来自处理程序函数的。请参阅event.preventDefault()与return false,以获得更完整的解释,但是基本上“ return false从jQuery事件处理程序中进行的操作实际上与在传递的jQuery.Event对象上调用e.preventDefault和e.stopPropagation相同。”

因此,上述代码的完整功能版本为:

getInfo = function() {
  $.ajax({
    url: '/test/info',
    dataType: 'html',
    success: function(data, textStatus) {
      if ( textStatus == 'success' ) {
        // doing stuff with data
      }
    }
  });
  return false;
};

// The order of vv that string may be important
$(document).on('click touchend', '.getInfo', getInfo );
Run Code Online (Sandbox Code Playgroud)