Ajax post 请求在 wp 主题目录中不起作用

Ajax post request doesn't work in wp theme directory

这是 ajax post 请求的示例。当放置在 wordpress 活动主题目录之外时它可以工作。但是,当它在那里并且 norefresh.php 也上传到那里时,无论我使用 norefresh.php 的确切补丁(site/themefolder/norefresh。php 或服务器补丁或本地补丁,它都不起作用补丁 norefresh.php 或 /norefresh.php)。根本不起作用。

关于 wordpress 有什么东西可以阻止我应该做的 execution.What 吗?

$.ajax({
type: "POST",
url: "norefresh.php",
data: reqdata,
cache: false,
success: function(html) {
//document.getElementById('myTextarea').value = html;
alert(html);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

您可以允许使用 htaccess 访问特定文件,但这对您的客户来说会很痛苦/如果您移动网站等?

最好只是从您的插件或主题中挂接该函数。那么你就不需要担心相对尿液了。 (在js中有一些细微的差别,但除此之外几乎是一样的)

add_action( 'wp_ajax_custom_hook', 'custom_function' );
add_action( 'wp_ajax_nopriv_custom_hook', 'custom_function' ); //-->front end hook...

function custom_function(){

    // handle code...
}

add_action('wp_head','define_ajaxurl'); //--> define ajaxurl using php so we can neatly place all js in js file

function define_ajaxurl() {
    ?>
    <script type="text/javascript">
    var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
    </script>
    <?php
}

JS

$.ajax({
    type: "POST",
    url: ajaxurl, //-->see php function hooked to wphead to define this!!
    //data: reqdata, //--> need also to pass the action....which is the hook!! 
    data: {
        action: 'custom_hook', //-->name of hook used as above..ie. wp_ajax_nameofhook...
        'otherfield': 'yea'
    }

    cache: false,
    success: function(html) {
    //document.getElementById('myTextarea').value = html;
    alert(html);
    }
});