1

I'm banging my head against updating an existing client site with lazy-loading content. At the moment the issue seems to be in the admin-ajax.php file, where the script dies prematurely on

if ( empty( $_REQUEST['action'] ) )
  die( '0' );

The error in console: GET http://www.example.com/wp-admin/admin-ajax.php?lang=en&action=example_frontpage_feed&page=0&next_time=0 net::ERR_EMPTY_RESPONSE

Everything works just fine on localhost. If I go straight to the url on browser, there's a flash of 'This page isn't working' screen but then it shows the right content. Also, if I change that piece on admin-ajax.php to anything else like die(1), the lazy-loader works just fin. That seems to me like a fix that might introduce some maintenance issues later, though.

Here's the ajax request:

fetchingFeed = true;
$.ajax({
  // url got from php doing basically this:
  // Example['ajax_url'] = admin_url( 'admin-ajax.php?lang=' . constant('ICL_LANGUAGE_CODE') )
  url: Example.ajax_url,
  dataType: 'json',
  type: 'get',
  data: { action: 'example_frontpage_feed', page: feedPage, next_time: feedNextTime },
  success: function(response) {
    if(response.success) {
      renderFeedPosts(response.posts);
      feedPage = response.nextPage;
      feedNextTime = response.nextTime;
    } else {
      if(response.error) {
        handleFeedLoadError(response.error);
      }
    }

    fetchingFeed = false;
  },
  error: function() {
    handleFeedLoadError();
    fetchingFeed = false;
  }
})

And here's the action

function example_frontpage_feed_ajax() {
  require_once 'inc/fetch-feed.php';

  $fetch_page = isset($_GET['page']) ? intval($_GET['page']) : 0;
  $fetch_next_time = isset($_GET['next_time']) ? $_GET['next_time'] : null;

  header('Access-Control-Allow-Origin: https://www.example.com');

  example_feed_content($fetch_page, $fetch_next_time);
}

add_action('wp_ajax_nopriv_example_frontpage_feed', 'example_frontpage_feed_ajax');
add_action('wp_ajax_example_frontpage_feed', 'example_frontpage_feed_ajax');

EDIT: The function example_feed_content basically gets posts and calls wp_send_json(blah blah)

3

In end of Ajax function you need to die() function

function example_frontpage_feed_ajax() {
  require_once 'inc/fetch-feed.php';

  $fetch_page = isset($_GET['page']) ? intval($_GET['page']) : 0;
  $fetch_next_time = isset($_GET['next_time']) ? $_GET['next_time'] : null;

  header('Access-Control-Allow-Origin: https://www.example.com');

  example_feed_content($fetch_page, $fetch_next_time);

  die(); // This is important in ajax
}

add_action('wp_ajax_nopriv_example_frontpage_feed', 'example_frontpage_feed_ajax');
add_action('wp_ajax_example_frontpage_feed', 'example_frontpage_feed_ajax');
  • I edited my post, forgot to mention that the example_feed_content calls wp_send_json which has the die in the end – Petri Oct 27 '17 at 14:08
  • @Petri Try it by another method, example echo json_encode( $array ); then die(); and its better if you put die(); in the end of original ajax function. – Behzad Oct 27 '17 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.