ajax重新加载页面,而不是在iOS上的Safari中执行ajax请求
我在本地apache实例(ajax重新加载页面,而不是在iOS上的Safari中执行ajax请求,ios,ajax,jquery,Ios,Ajax,Jquery,我在本地apache实例(/test)的一个页面上调用了一个函数,该实例使用jQuery.ajax调用子页面(/test/info),并正确地进行ajax调用,并在FF、Safari、Chrome和iOS emulator中从我桌面上的响应动态加载内容,未进行任何调用,页面将刷新 window.getInfo = function ( event ) { console.log('1) prior to $.ajax'); $.ajax({ url: 'http://localh
/test
)的一个页面上调用了一个函数,该实例使用jQuery.ajax
调用子页面(/test/info
),并正确地进行ajax调用,并在FF、Safari、Chrome和iOS emulator中从我桌面上的响应动态加载内容,未进行任何调用,页面将刷新
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'); });
};
从桌面浏览器打印所有日志,我的apache服务器在/test
报告一个请求,但在iOS emulator中的Safari上,仅打印$.ajax'之前的'1)和'2)beforeSend'
日志,向apache发出的下一个请求是/test
有没有人知道这里发生了什么,以及如何让iOS正常运行
更新:当我向ajax调用添加async:false
属性时,所有日志都会打印出来,并发出请求,因此基本上解决了这个问题;但是,页面仍然会重新加载,我认为这是与iOS上的事件传播相关的另一个问题。要使此工作正常,只需返回false处理程序函数中的代码>。有关更完整的解释,请参阅,但基本上,“从jQuery事件处理程序中返回false
实际上与对传递的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 );
触发机制是什么?$(document).on('touchend','.getInfo',函数(事件){getInfo(事件);});