2014-04-01 60 views
0

我通过jquery/ajax动态加载php文件到Wordpress页面模板中。Wordpress,jquery加载php文件未找到

我已经得到了以下工作在本地服务器上,但是当我上传到我的测试网站在线时,我在加载文件时在控制台中出现404错误。

总结代码:

var root = location.protocol + '//' + location.host; 

$(".button-book").click(function(e) { 
    e.preventDefault(); 
    $('#container').load(root+'/wp-content/themes/PL14-Base/inc/bookings-swiss.php'); 
}); 

实际上,你可以看到网站的发展here。点击第一个“预订”按钮查看问题。

更新: 我已经改变使用完全相同的网址为清楚起见 该文件可以在the correct url发现,当在浏览器中直接调用的代码。

+0

仔细检查网址,可能问题出在哪里。 –

+0

@NetaMeta我可以在浏览器中成功加载url,只需动态调用即可获得404。 –

+0

是同一主机上的网址? –

回答

0

这是在一个PHP文件?如果是的话请使用get_template_directory_uri();帮手

var root = location.protocol + '//' + location.host; 

$(".button-book").click(function(e) { 
    e.preventDefault(); 
    $('#container').load('<?php echo get_template_directory_uri(); ?>/inc/loaded-file.php'); 
}); 
+0

不,它在javascript中 –

+0

js文件位于'wp-content/themes/PL14-Base/library/js /'中,并通过'functions.php'调用到网站的页脚 –

0

我已经设法通过在加载的PHP文件的顶部使用<?php require($_SERVER['DOCUMENT_ROOT'].'/wp-load.php'); ?>得到这个工作,但是这是一个肮脏的修复。宁愿知道这样做的正确方法。

+0

你想要达到什么?你想找到一种方法来做ajax调用WP的正确方式吗? –

+0

@NetaMeta部分是的,但它比这更复杂一点。我为了使用wpml插件翻译内容而动态调用一个包含多个Wordpress GetText回声调用'_e()'的PHP文件。 –

+0

你知道如何为WP设置普通的ajax“路由器”吗?正确的方式 ?我问,因为我不知道你的问题,但它似乎是你试图做ajax –

0

在你主插件或功能的PHP或任何你会做以下,以一个全局变量附加到某个脚本

$main_js_namespace = array(
    'ajaxURL' => admin_url('admin-ajax.php'), 
    'data' => array(
     'action' => '', 
     'method' => '', 
     'post' => '', 
    ) 
); 

// script handler, javascript global variable name, 
wp_localize_script('bec_script_handle','gVariable',$main_js_namespace); 

该类类处理Ajax调用。

class ajaxClass { 
    public function __construct() { 
     // ajaxClass can be whatever you want.. 
     add_action('wp_ajax_nopriv_ajaxClass', array($this, 'handle_ajax')); 
     add_action('wp_ajax_ajaxClass', array($this, 'handle_ajax')); 
    } 

    // this simply handle the routing of the functions. 
    public function handle_ajax() { 
     if(isset($_POST['method'])) { 
      $method = $_POST['method']; 
      if(method_exists($this, $method)) { 
       if(isset($_POST['post']) && $_POST['post'] != 'false') { 
        parse_str(stripslashes($_POST['post']), $post); 
        $request = call_user_func(array($this, $method), $post); 
        echo (is_array($request)) ? json_encode($request) : $request; 
       } else { 
        $request = call_user_func(array($this, $method)); 
        echo (is_array($request)) ? json_encode($request) : $request; 
       } 
      } else { 
       $json['success'] = false; 
       $json['msg'] = __CLASS__.'::'.$method.' not found, define first! por favor..'; 
       echo json_encode($json); 
      } 
     } 
     exit; 
    } 

    /* 
    * bla 
    * */ 
    public function someAjaxCall($post){ 
     $toRetun = "whatever Response You Want"; 
     return $toRetun; 
    } 

} 

$ajaxClass = new ajaxClass(); 

//这是我怎么会打的电话到该脚本

// the action we chose 
    gVariable.data.action = 'ajaxClass'; 

    // whatever method in the ajax class you want to access 
    gVariable.data.method = 'get_table_schema'; 

    // whatever variable/data you want to pass 
    gVariable.data.post = 'tableName=' + tableName; 

    this functions is a async false and ones done give you an object with the result however any other would work. 
    var getData = function(){ 
     var getData = {}; 
     var setData = function(data) { 
      getData = data; 
     }; 

     return function(){ 
      $.ajax({ 
       url: gVariable.ajaxURL, 
       type: "post", 
       async: false, 
       dataType:'json', 
       data:becGlobal.data, 
       success: function (data) { 
        setData(data); 
       }, 
       error : function (data) { 
        setData(data); 
       } 
      }); 
      return getData; 
     } 
    } 

    getData()(); 
</script> 
+0

谢谢@NetaMeta,但那远远超出了我。我想我需要首先了解更多。 –

+0

这是非常基本的。只是看起来很复杂,不过适合自己 –

+0

我很感激帮助,我不能将这个应用于我的情况。这就是说,我没有在我原来的问题中清楚地定义整个问题。我在这里详细介绍了它:http://wordpress.stackexchange.com/questions/139896/load-a-form-via-jquery-ajax-that-c​​ontains-strings-for-translation-wpml我会查看Wordpress AJAX API以供参考,并尝试使用您的示例 –