0

I'm trying to get this W3 Schools example to work in WordPress.

I already tried this in function.php:

function hook_ajax_script(){
        wp_enqueue_script( 'my_ajax_script', get_bloginfo('template_url')."/rscripts/robajax.js" );
    }
    add_action( 'wp_enqueue_scripts', 'hook_ajax_script' );
    add_action( 'admin_enqueue_scripts', 'hook_ajax_script' );

add_shortcode( 'ajax1', 'ajax1_shortcode' );
    function ajax1_shortcode( $args ) {
    ?>
    <div id="demo">
    <h1>The XMLHttpRequest Object</h1>
    <button type="button" onclick="loadDoc()">Change Content</button>
    </div>

and my Script

jQuery(document).ready(function(){
  jQuery("button").click(function(){
        jQuery.ajax({url: "rob.txt", success: function(result){
            $("#div1").html(result);
        }});
    });
});

When I click the Button I get 404 "rob.txt" not found.

I tried it also to do it with a Plugin and rob.txt was inside.

How to get this example to work?

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Rob
  • 1
  • 2
  • Since you're not passing an absolute URL, your script will try to load a file called `rob.txt` located at the root of the current page (eg. `http://www.example.com/wp-admin/admin.php?page=your-custom-admin-page` or `http://www.example.com/page-with-short-code/`) and of course it won't find it, hence the 404 error message. – cabrerahector Jul 26 '18 at 14:41
  • Oh Sorry, i edit the post.... i post the wronge code. tried to much ...:-) – Rob Jul 26 '18 at 14:48
  • What I said above still applies, though. – cabrerahector Jul 26 '18 at 14:58
  • You're missing many important parts. The duplicate questions should give you a better start. It's important to check the guides [ask] and [mcve] before posting questions here. Good luck! – brasofilo Jul 26 '18 at 21:56

0 Answers0