我的插件页面中有此操作。
add_action ( \'init\', \'myStartSession\', 1 );
function join_action() {
$a = 1;
include "includes/join.php";
}
add_action ( \'admin_post_nopriv_join\', \'join_action\' );
add_action ( \'admin_post_join\', \'join_action\' );
我从angularjs应用程序中调用它,如下所示:
$scope.join = function() {
$scope.formData.action = "join";
$http(
{
method : \'POST\',
url : \'<?php echo MEMBERSHIP_APP; ?>\',
data : jQuery.param($scope.formData), // pass in data as strings
headers : {
\'Content-Type\' : \'application/x-www-form-urlencoded\'
}
})
.success(
function(data) {
console.log(data);
if (!data.success) {
// if not successful, bind errors to error variables
} else {
// if successful, bind success message to message
$scope.message = data.message;
}
});
};
其中MEMBERSHIP\\u APP指向wp admin/admin ajax。php
请你看看我做错了什么。我以前多次使用过这个结构。
SO网友:Den Pat
在javascript代码中,在动作名称后添加\\u动作。例如:
$scope.formData.action = "join_action";
和重命名函数,从中删除操作,如:
function join() {
$a = 1;
include "includes/join.php";
}
如果您想要工作示例,请参见:
add_action(\'wp_ajax_your_function_name_action\',\'your_function_name_ajax\')
add_action(\'wp_ajax_nopriv_your_function_name_action\',\'your_function_name_ajax\')
if (!function_exists(\'somename\')) {
function somename(){
echo $_POST[\'eidvar\'];
exit();
}
}
var data = {
action: \'your_function_name_action\',
eidvar: "somevalue",
}
$.post(MEMBERSHIP_APP, data).done(function(response) {
if(response.indexOf("no") > -1) {
//no result etc
} else {
console.log(response)}
});