0

Adding die(); to the end of my ajax function file gives me totally blank page and this error in Firebug:

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must to be declared in the document or in the transfer protocol.

I scaled down to a very basic ajax function file:

<?php
function show_article(){
$q = $_GET['getid'];

echo strval($q);
}

die();
?>

Which still gives the empty page and error. Without die(); at the close, it works as expected but with the extra 0 at the end of the echoed $q value.

I'm not sure what is causing this issue. For extra context, here's what I've got in my functions.php file.

function ajaxscripts() {
wp_enqueue_script( 'productajax', get_template_directory_uri().'/js/productajax.js', false, null, false );  
wp_localize_script( 'productajax', 'myajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_enqueue_scripts', 'ajaxscripts');

$dirName = dirname(__FILE__);
$baseName = basename(realpath($dirName));
require_once ("$dirName/getarticle.php"); //my ajax function file

add_action( 'wp_ajax_nopriv_show_article', 'show_article' );
add_action( 'wp_ajax_show_article', 'show_article' );

2 Answers 2

0

The die should be in your show_article() function. Yours is out, so the whole script is terminated. Your function should be:

function show_article(){
  $q = $_GET['getid'];
  echo strval($q);
  die;
}

or

function show_article(){
  $q = $_GET['getid'];
  exit( strval($q) );
}
3
  • You can always edit existing answers if you think they are lacking... Jun 6, 2013 at 13:20
  • Argh. So simple! Being new to developing, I'm not yet as vigilant with my semicolons and brackets as I need to be. Thank you.
    – cryssybee
    Jun 6, 2013 at 18:14
  • @StephenHarris Noted :)
    – RRikesh
    Jun 7, 2013 at 5:13
0

Your die() is outside the function show_article().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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