0

i create ajax request in functions.php

function my_enqueue() {
    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/script.js', array('jquery') );
    wp_localize_script( 'ajax-script', 'cc_ajax_object', 
        array( 'ajax_url' => admin_url( 'ajaxrequest.php' ) ) );
}

add_action( 'wp_enqueue_scripts', 'my_enqueue' );

add_action( 'wp_footer', 'add_js_to_wp_footer' );
function add_js_to_wp_footer()
{ 
   ?>
   <script type="text/javascript">
   jQuery('.all-categories').click(function($){
     jQuery.ajax({
       type : "post",
       dataType : "json",
       url : cc_ajax_object.ajax_url,
       data: {
        'action': 'get_products',        
      },
      success:function(data) {
        // This outputs the result of the ajax request
        console.log(data);
      },
      error: function(errorThrown){
         console.log(errorThrown);
      }
   });
  });
  </script>

and in ajaxrequest.php

<?php 
 /* ajaxrequest page */
 function get_products()
 {
    echo "test";
 }

no errors in network but data = null in console

whats is the mistake ????

  • you should use ajax url array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); and include ajaxrequest.php file in theme's functions.php file and in ajaxrequest.php file you have to add add_action for wp_ajax and wp_ajax_nopriv_ . – Chetan Vaghela Apr 28 '20 at 10:51
  • where include ajaxrequest.php file, plz?? – Omnia Magd Apr 28 '20 at 11:00
  • currently where did you added file ? you should add file in active themes folder.and include file using require get_template_directory() . '/ajaxrequest.php'; path of file in themes functions.php – Chetan Vaghela Apr 28 '20 at 11:14
  • i change ajaxrequest.php to admin-ajax.php and in admin-ajax add this code add_action( 'wp_ajax_get_products', 'get_products' ); add_action( 'wp_ajax_nopriv_get_products', 'get_products' ); and insert ajaxrequest.php file to the theme. what must do then??? – Omnia Magd Apr 28 '20 at 11:46
  • I have added an answer. please check – Chetan Vaghela Apr 28 '20 at 11:55
1

You can take reference from below code. Remove your ajaxrequest.php file, no need of that file because i have added ajaxrequest.php file's code in active theme's functions.php file.

I have changed ajax url and add js script code in script.js file. I have tested and it is working for me. let me know if this works for you.

functions.php

function my_enqueue() {
    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/script.js', array('jquery') );
    wp_localize_script( 'ajax-script', 'cc_ajax_object', 
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

add_action( 'wp_enqueue_scripts', 'my_enqueue' );


add_action('wp_ajax_get_products', 'get_products' ); // executed when logged in
add_action('wp_ajax_nopriv_get_products', 'get_products' ); // executed when logged out
function get_products()
{
  echo "test";
  wp_die();
}

script.js

jQuery(document).ready(function($) {
   jQuery('.all-categories').click(function($){
     jQuery.ajax({
       type : "post",
       dataType : "json",
       url : cc_ajax_object.ajax_url,
       data: {
        'action': 'get_products',        
      },
      success:function(data) {
        // This outputs the result of the ajax request
        console.log(data);
      },
      error: function(errorThrown){
         console.log(errorThrown);
      }
   });
  });
});
  • 1
    it works , thanks for your help – Omnia Magd Apr 28 '20 at 12:04
  • You’re welcome! – Chetan Vaghela Apr 28 '20 at 12:08
0

Forget about the separate ajaxrequest.php file. Your error is in the wp_localize_script, you need to point to the admin-ajax.php file when you localize the script:

function my_enqueue() {
    wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/script.js', array('jquery') );
    wp_localize_script( 'ajax-script', 'cc_ajax_object', 
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

add_action( 'wp_enqueue_scripts', 'my_enqueue' );

Another thing is about the function that will manage the response, instead of requiring it in your theme function.php file, put the code directly in the theme function file.

  • this answer not useful. – Omnia Magd Apr 29 '20 at 14:30

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.