简体   繁体   English

Ajax呼叫网址wordpress

[英]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 此文件中content / themes / themename / js / main.js中的js文件,我正在尝试对该文件content / themes / themename / lib / file.php进行ajax调用

this is how my ajax call looks like 这就是我的ajax调用的样子

    $.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 我也尝试过../lib/file.php

maybe you can try with the path that relative to the root directory ex : 也许您可以尝试使用相对于根目录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 我通过在header.php文件的body标签中执行此操作来修复它

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

and use the url in my javascript like this 并像这样在我的javascript中使用URL

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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