2016-04-21 111 views
1

这是ajax发布请求的示例。当它放置在wordpress活动主题目录之外时它工作。然而,当它在那里,并且norefresh.php也被上传时,无论norefresh.php使用什么确切的补丁(site/themefolder/norefresh.php或服务器补丁或本地补丁norefresh.php或/ norefresh),它都不起作用。 PHP)。根本不起作用。Ajax发布请求在wp主题目录中不起作用

有什么关于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>

回答

3

可以允许访问使用htaccess的具体文件,但/这将是一个痛苦为您的客户,如果你移动网站等?

更好的是从你的插件或主题挂钩功能。那么你不需要担心相对的uris。 (一些细微的差别在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); 
    } 
});