WordPress的,jQuery的加载PHP文件未找到 [英] Wordpress, jquery load php file not found

查看:85
本文介绍了WordPress的,jQuery的加载PHP文件未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过jquery/ajax将php文件动态加载到Wordpress页面模板中.

I'm loading php files dynamically via jquery/ajax into a Wordpress page template.

我在本地服务器上具有以下功能,但是当我在线上传到测试站点时,加载文件时控制台中出现404错误.

I've got the following which works on a local server but when I upload to my test site online I get a 404 error in the console when loading the file.

摘要代码:

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');
});

您实际上可以在此处看到开发站点.点击第一个进行预订"按钮以查看问题.

You can actually see the development site here. Click the first 'Make a reservation' button to see the issue.

更新: 为了清楚起见,我更改了代码以使用确切的网址 可以在正确的网址中找到文件直接在浏览器中调用时.

Update: I've changed the code to use the exact urls for clarity The file can be found at the correct url when called directly in the browser.

推荐答案

在您的主插件或函数php中,或者在您将要执行的以下操作中,将全局变量附加到特定脚本中

In you main plugin or function php or wherever you'll do the following to attach a global variable to a certain script

$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调用.

This class class handle the ajax calls.

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();

//这就是我如何调用该脚本的方式

// this is how i would make the calls to that script

    // 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>

这篇关于WordPress的,jQuery的加载PHP文件未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆