Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:23:49+00:00 2026-06-05T17:23:49+00:00

Disclaimer: not sure this is WordPress related or not. I’m following a simple tutorial

  • 0

Disclaimer: not sure this is WordPress related or not.

I’m following a simple tutorial to check if AJAX works in my WordPress localhost.

My ajax-test.js :

jQuery(document).ready( function($){
    $(".ajax-link").click( function(){
        var data = {
            action: 'test_response',
            post_var: 'this will be echoed back'
        };
        $.post(the_ajax_script.ajaxurl, data, function(response) {
                                                       alert(response);
                                                       });
        return false;
    });
});

In my plugin, I add the script to the wp_print_scripts() and add the processing function:

function test_ajax_load_scripts(){
    wp_enqueue_script("ajax-test", plugin_dir_url( __FILE__ ) . 'ajax-test.js', array( 'jquery' ) );
    wp_localize_script('ajax-test', 'the_ajax_script', array( 'ajaxurl' => admin_url('admin-ajax.php') ) );
}
add_action('wp_print_scripts', 'test_ajax_load_scripts');

function text_ajax_process_request(){ 
    if(isset($_POST["post_var"])){
        $response = $_POST["post_var"];
        echo $response;
        die();
    }
}
add_action('wp_ajax_test_response', 'test_ajax_process_request');

I get the link output with the href=”#” and I click it and it brings me to the top of the page. I check Firebug and I can confirm that the ajax-test.js is loaded in the header. There is nothing in the console.

How else can I debug why the alert is not happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:23:52+00:00Added an answer on June 5, 2026 at 5:23 pm

    Your event wire-up function needs an e.preventDefault() in it:

    $(".ajax-link").click( function(e){ //<--------- e
        e.preventDefault(); //<-------- preventDefault()   
        var data = {
            action: 'test_response',
            post_var: 'this will be echoed back'
        };
        $.post(the_ajax_script.ajaxurl, data, function(response) {
            alert(response);
        });        
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Disclaimer: I'm fairly new to AJAX! I've looked around, and I'm not sure which
Disclaimer: I'm a total newbie at graph theory and I'm not sure if this
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This is for a programming class, but it is not the answer or
Disclaimer : I haven't done web programming for ages and am not even sure
Disclaimer: I have already seen the following questions and their solutions did not apply
Disclaimer: I am anything but a Javascript expert, so I'm not even sure if
Disclaimer: please do not bother reading this long post unless you are an embedded
Disclaimer: Do not try this at home Why, if I am using jQuery, does
First off, I need to give a disclaimer: DO NOT RUN THIS JAVASCRIPT It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.