• Hello

    I am relatively new to WP, and I am stuck for two days with an Ajax problem. I am trying to do a very simple ajax call from the client to the server (I have a local WP installation)

    On the server, I have the following PHP code (embedded in a plugin main file)

    PHP in main plugin file:

    function adding_scripts() {
    
    wp_register_script('expenseTableDef', plugins_url('js/expenseTableDef.js', __FILE__),
      array('jquery','jquery-ui-selectable'),'1.191', true);
      wp_enqueue_script('expenseTableDef');
      wp_localize_script(  'expenseTableDef',  'expenseTableDef_ajax_obj',
      array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ));
    
    }
    
    add_action( 'wp_enqueue_scripts', 'adding_scripts' );
    
    function mcp_categories() {
      echo 'from server';
      wp_die();
    }
    
    add_action( 'wp_ajax_mcp_categories','mcp_categories');
    add_action( 'wp_ajax_nopriv_mcp_categories','mcp_categories');

    and this is the Js file expenseTableDef.js:

    
    jQuery(document).ready( function () { ;
     load_categories() ;
    } );
    
    function load_categories() {
    
    		var data = {
    			'action': 'mcp_categories',
    			'whatever': 1234
    		};
    
    		jQuery.post(expenseTableDef_ajax_obj.url, data, function(response) {
    			console.log('Got this from the server: ' + response);
    		});
    }

    All js files are enqueued properly and I can find them in the source code of the page. no matter what I do, and no matter what working example I use from the web, the response is the Html code of the page I am currently viewing.

    I am very grateful for the help I can get here.

    Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • In your jQuery.post call, you refer to expenseTableDef_ajax_obj.url, but your wp_localize_script call has ajaxurl.

    Thread Starter mcpab68

    (@mcpab68)

    I thank you so very much. After you keep looking at the same code for a long time the easiest mistake gets lost. Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Ajax Issue – Local WP’ is closed to new replies.