简体   繁体   中英

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

    $.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

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);
    }
});

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',
 $.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);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM