WordPress AJAX Call Not Return Result

I’m new to WP and not overly familiar with AJAX either but I’m trying to get a very simple AJAX call to work, I think I’m nearly there but I can’t get any results to return back from the AJAX call, I’ve stripped everything down to the bare bones to get it working and am just returning the string ‘This is AJAX data results’ from the PHP Ajax function but even that does not appear to be working. At the moment I get my test alerts…

alert(‘jquery step 1’); – Works OK

alert(‘jquery step 2’); – Works OK

alert(‘ajax result: ‘.response); – ** NOT DISPLAYED **

alert(‘jquery step 3’); – Works OK

I’ve created a custom plugin and custom js and php files as follows…

********* c4l-custom-functions.php ********

* Plugin Name: C4L Custom Functions Plugin
* Description: This plugin contains C4l custom functions, scripts and css
* Author: C4L
* Version: 1.0

function c4l_custom_script_and_style_includer() {
wp_enqueue_script( ‘c4l-js’, plugins_url( ‘js/c4l-custom-scripts.js’ , __FILE__ ) );
wp_enqueue_style( ‘c4l-css’, plugins_url( ‘css/c4l-custom-styles.css’ , __FILE__ ) );

add_action( ‘wp_enqueue_scripts’, ‘c4l_custom_script_and_style_includer’ );

add_action( ‘wp_ajax_wps_get_time’, ‘wps_get_time’ );
add_action( ‘wp_ajax_nopriv_wps_get_time’, ‘wps_get_time’ );

function wps_get_time() {
// $format = $_POST[‘format’];
echo(‘This is AJAX data results’);
//echo date($format);

********* c4l-custom-scripts.js ********

document.addEventListener(“DOMContentLoaded”, function(event) {
alert(‘jquery step 1’);
var timeformat = ‘U’;
alert(‘jquery step 2’);
type: “POST”,
url: “admin-ajax.php”,
data: { action: ‘wps_get_time’, format: timeformat },
success: function ( response ) {
alert(‘ajax result: ‘.response);
alert(‘jquery step 3’);

Read more here:: WordPress AJAX Call Not Return Result

Leave a Reply

Your email address will not be published. Required fields are marked *