我有一个API的ajax调用,并根据我想重定向我的用户的响应,API调用是非常基本的,工作正常:
jQuery(document).ready(function($){
$.ajax({
url: '//apicall-url',
dataType: 'json',
async: false,
success: function(obj) {
//redirect goes here
}
});
});
我的问题是当前的wordpress页面正在加载,然后一旦ajax调用“成功”进行重定向任何有关如何正确的建议(我是WP新手)?
我也尝试过使用wp_enqueue_script函数,但结果相同。
我试图在标题和正文中这样做,没有运气。
您需要在页面和脚本加载之前进行api调用。尝试从PHP端调用您的API。看看template_redirect
钩。这是创建重定向的最佳钩子。
我的问题是“文件就绪”等待ajax调用改变这个工作正常,这是我的代码:
(function($) { //<-This is an anonymous function that instantaneously runs.
$.ajax({
url: '//apicall-url',
dataType: 'json',
async: false,
success: function(obj) {
//redirect goes here
}
});
})( jQuery ); //<----passing jquery to avoid any conflict with other libraries.