这是一个ajax post请求的例子。当放置在 WordPress 事件主题目录之外时它可以工作。然而,当它在那里并且 norefresh.php 也上传到那里时,无论我使用 norefresh.php 的确切补丁(site/themefolder/norefresh.php 或服务器补丁或本地补丁 norefresh.php 或/norefresh.php),它都不起作用。 php)。根本不起作用。
wordpress 是否有任何内容阻止执行。我该怎么办?
$.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 允许访问特定文件,但这会给您的客户带来痛苦/如果您移动网站等?
最好从您的插件或主题中 Hook 该函数。那么你就不需要担心相对uri。 (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);
}
});
关于jquery - Ajax post请求在wp主题目录中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36782249/