1

Regarding to this post I started to create an AJAX handler with the default WordPress AJAX API. I want to call a non-blocking call to another AJAX handler, in the same WordPress site.

Order of processes:

  1. jQuery.ajax() to the wp-admin/admin-ajax?action=x_prepare
  2. wp_ajax_x_prepare() uses the wp_remote_get() function
  3. $response = wp_remote_get(admin_url("admin-ajax.php?action=x_start"), array( "blocking" => false ));
  4. wp_ajax_x_start() is doing any not-relevant code for this question...

Then i get the following error from the wp_remote_get() function.

Array ( [headers] => Array ( ) [body] => [response] => Array ( [code] => [message] => ) [cookies] => Array ( ) [http_response] => ) 0

cURL is activated.

EDIT

Just found this post: Passing current cookies in wp_remote_get to get Draft Post Preview

When I try the same code:

$preview_link = set_url_scheme( get_permalink( 7250 ) );
$preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ) ) );
var_dump($preview_link);
$cookies      = array();

foreach ( $_COOKIE as $name => $value ) {
    $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
}

$request = wp_remote_get( $preview_link, array( 'cookies' => $cookies ) );
$body    = wp_remote_retrieve_body( $request );

... I get this error Missing header/body separator:

WP_Error#835 (2) { public $errors => array(1) { 'http_request_failed' => array(1) { [0] => string(29) "Missing header/body separator" } } public $error_data => array(0) { } } 0
  • 1
    This is wrong approach. Call the functions directly instead of emulating AJAX request as you are needlessly loading your server. As for why you get an error, it is probably because that ajax returns json and not html – Mark Kaplun Aug 26 '16 at 9:08
  • As @MarkKaplun said, you're firing an AJAX request to the server, which then fires another HTTP request back to itself - just cut that part out and return the response you need for jQuery/JavaScript. – TheDeadMedic Aug 26 '16 at 9:56

Your Answer

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

Browse other questions tagged or ask your own question.