简体   繁体   English

在ajax调用完成后加载wordpress页面

[英]Load wordpress page after an ajax call finish

I have an ajax call to an API and based on the response I want to redirect my users, the API call is pretty basic and works fine: 我有一个对API的ajax调用,根据我想重定向用户的响应,该API调用非常基础,并且可以正常工作:

    jQuery(document).ready(function($){
      $.ajax({
        url: '//apicall-url',
        dataType: 'json',
        async: false,
        success: function(obj) {
           //redirect goes here
        }
      });
    }); 

My problem is that the current wordpress page is loading and then doing the redirect once the ajax call is "success" any suggestion on how to to this correctly (I'm a WP newbie)? 我的问题是当前的wordpress页面正在加载,并且一旦ajax调用“成功”就如何正确地进行此操作的任何建议(我是WP新手),都将进行重定向。

I also tried using the wp_enqueue_script function but got the same result. 我也尝试使用wp_enqueue_script函数,但得到了相同的结果。

I tried to do this in the header and body with no luck. 我尝试在标头和正文中执行此操作,但没有运气。

You need do your api call before page and scripts will be loaded. 您需要先进行api调用,然后才能加载页面和脚本。 Try call your API from PHP side. 尝试从PHP端调用您的API。 Look template_redirect hook. template_redirect钩子。 This is the best hook to create redirects. 这是创建重定向的最佳方法。

My issue here was the "document ready" waiting for the ajax call changing this worked fine, here is my code: 我的问题是等待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.

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

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