Jquery Ajax调用在wordpress中添加当前url

Jquery Ajax调用在wordpress中添加当前url,jquery,ajax,wordpress,wordpress-theming,Jquery,Ajax,Wordpress,Wordpress Theming,当我试图在wordpress js文件中使用jquery执行ajax调用时,它会自动重定向到当前路径,然后附加我的自定义路径,这样我就无法重定向我的真实URL 这是我的密码: var path_test = document.location.hostname + '/wpcontent/themes/expression/imagecount.php'; var path; $.ajax({ url: path_test, type: "GET", data: '30' }).

当我试图在wordpress js文件中使用jquery执行ajax调用时,它会自动重定向到当前路径,然后附加我的自定义路径,这样我就无法重定向我的真实URL

这是我的密码:

var path_test = document.location.hostname + '/wpcontent/themes/expression/imagecount.php';
var path;
$.ajax({
   url: path_test,
  type: "GET",
  data: '30'
 }).done(function() {
    alert('ajax call success');
});

它添加路径,但首先添加当前URL,然后添加我的URL,这样ajax调用就会失败。

对于任何ajax调用,您都应该参考codex

  • 即使您没有在插件中运行它

    服务器端

    add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
    add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
    
    function prefix_ajax_add_foobar() {
        // Handle request then generate response using WP_Ajax_Response
    
        // Here you would fetch and process desired file.
    }
    
    jQuery.post(
        ajaxurl, 
        {
            'action': 'add_foobar',
            'data':   'foobarid'
        }, 
        function(response){
            alert('The server responded: ' + response);
        }
    );
    
    客户端

    add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
    add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
    
    function prefix_ajax_add_foobar() {
        // Handle request then generate response using WP_Ajax_Response
    
        // Here you would fetch and process desired file.
    }
    
    jQuery.post(
        ajaxurl, 
        {
            'action': 'add_foobar',
            'data':   'foobarid'
        }, 
        function(response){
            alert('The server responded: ' + response);
        }
    );
    

    在适当的设置下,您不想直接从主题文件夹调用文件,最好有一个单一的访问点,您可以加载所需的文件并返回正确的响应。

    ,因为您缺少
    位置。协议
    (http:)。 试试这个:

    var path_test = location.protocol + '//' + document.location.hostname + '/wpcontent/themes/expression/imagecount.php';
    

    你在什么情况下运行这个代码?@abdulwired,我已经重新测试过了,看起来还可以。当您在Firefox或Chrome“开发者工具”中检查“网络监视器”时,会发送什么请求?