函数名和ajax操作回调名称应该相同。
add_action(\'wp_ajax_pdfAction\',\'pdfAction_callback\');
add_action(\'wp_ajax_nopriv_pdfAction\',\'pdfAction_callback\');
function pdfAction_callback(){
$post = $_POST[\'pdfAction\'];
echo "something";
wp_die();
}
请使用上述内容更新您的php代码。
目前,您的php函数尚未执行。
Update:
Ajax脚本,
$(document).on(\'click\', "#generate-pdf", function(){
var price1 = $("#price1").val();
var price2 = $("#price2").val();
$.ajax({
url: ajax_object.ajax_url, // AJAX handler
data: {
\'action\': \'pdfAction\',
\'price_1\': price1,
\'price_2\': price2
},
type: \'POST\',
success: function(data) {
console.log(data);
console.log(data.price_1) // to access individual values
},
error: function(errorThrown){
console.log(errorThrown);
}
});
return false;
});
注册Ajax操作Url和脚本,
function ajax_enqueue_scripts() {
// Enqueue your javascript file
wp_register_script( \'ajax-scripts\', get_template_directory_uri() . \'/js/ajax-scripts.js\', array(\'jquery\'), \'1.0.0\', true );
// in JavaScript, object properties are accessed as ajax_object.ajax_url
wp_localize_script( \'ajax-scripts\', \'ajax_object\', array(
\'ajax_url\' => admin_url( \'admin-ajax.php\' ), // admin ajax url
));
wp_enqueue_script(\'ajax-scripts\');
}
add_action( \'wp_enqueue_scripts\', \'ajax_enqueue_scripts\' );
Ajax请求回调函数,
add_action(\'wp_ajax_pdfAction\',\'pdfAction_callback\');
add_action(\'wp_ajax_nopriv_pdfAction\',\'pdfAction_callback\');
function pdfAction_callback(){
// Getting data sent from Ajax request
$price_1 = $_POST[\'price_1\'];
$price_2 = $_POST[\'price_2\'];
// Prepare Data to be send back to AJAX request
$return = array(
\'price_1\' => $price_1,
\'price_2\' => $price_2
);
wp_send_json($return); // Send JSON response back to our AJAX Request and die().
// we don\'t require wp_die(); here wp_send_json() will take care of it.
}
为了安全起见,您还应该考虑在此处使用nonce。