我有一个桌面应用程序,我使用$.getJSON 要使用插件从我的wordpress站点获取数据WP REST API. 这很好,因此我假设我也可以对函数中的函数进行Ajax调用。php?我该怎么做?我所看到的示例都没有提供ajaxURL。我怎么知道这是什么?
我有以下代码:
Functions.php (in wordpress site on server)
//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally
function folder_contents() {
$folderPath = $_POST[\'path\'] ;
$files = array_diff(scandir($path), array(\'.\', \'..\'));
print_r($files);
return $files;
die();
}
add_action(\'wp_ajax_my_action\', \'folder_contents\');
app.js (run using node webkit locally from machine)
//ajax call
var data = {
action: \'path\',
path: datafolder;
};
var ajaxurl = baseurl + \'\'; //WHAT IS THIS?!?!
jQuery.post(ajaxurl, data, function(response) {
alert(\'Got this from the server: \' + response);
console.log(response);
});
-------------------EDIT----------------------
在回答了下面的问题后,我根据建议尝试了下面的代码,但我得到了
“index.html:324未捕获引用错误:未定义ajax\\u参数”
Functions.php
//add code to use ajax
function add_ajax_script() {
wp_localize_script( \'ajax-js\', \'ajax_params\', array( \'ajax_url\' => admin_url( \'admin-ajax.php\' ) ) );
}
//Called by Ajax from App - list the contents of the folder so they can be downloaded and stored locally
function folder_contents() {
$folderPath = $_POST[\'path\'] ;
$files = array_diff(scandir($path), array(\'.\', \'..\'));
wp_send_json($files);
die();
}
add_action(\'wp_ajax_folder_contents\', \'folder_contents\');
add_action(\'wp_ajax_nopriv_folder_contents\', \'folder_contents\');
add_action( \'wp_enqueue_scripts\', \'add_ajax_script\' );
App.js
//ajax call
var data = {action: \'powerpoint_folder_contents\',path: datafolder};
var ajaxurl = ajax_params.ajax_url;
console.log(ajaxurl);
jQuery.post(ajaxurl, data, function(response) {
alert(\'Got this from the server: \' + response);
console.log(response);
});
如果我删除
add_ajax_script() 函数,只需使用下面的返回0。
var ajaxurl=baseurl+\'wp admin/admin ajax。php’;
请帮助。。。