ajax call url wordpress

js file in content/themes/themename/js/main.js
in this file I am trying to do an ajax call to this file content/themes/themename/lib/file.php

this is how my ajax call looks like

Read More
    $.ajax({
    type: 'POST',
    url: 'file.php',
    success: function(result) {
        var data = jQuery.parseJSON(result);
        console.log(data.name);
        }
    });

but the url is not working anyone can help me out?

I have tried ../lib/file.php too

Related posts

3 comments

  1.  $.ajax({
        type: 'POST',
        url: '<?php echo get_stylesheet_directory_uri();?>/lib/file.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            console.log(data.name);
            }
        });
    
  2. maybe you can try with the path that relative to the root directory
    ex :

    $.ajax({
        type: 'POST',
        url: '/wordpress_site/wp-content/themes/themename/lib/file.php',
        success: function(result) {
            var data = jQuery.parseJSON(result);
            console.log(data.name);
        }
    });
    
  3. I fixed it by doing this in my body tag of my header.php file

    data-theme-url="<?php echo get_stylesheet_directory_uri(); ?>
    

    and use the url in my javascript like this

    url: $('body').data('theme-url') + '/lib/file.php',
    

Comments are closed.