努力创建ajax请求。我已经读了几篇文章,但仍然无法让我的插件发送和接收ajax数据。
首先初始化js
function ajax_load_scripts() {
// load our jquery file that sends the $.post request
wp_enqueue_script( "ajax-calls", plugin_dir_url( __FILE__ ) . \'/assets/js/ajax_calls.js\', array( \'jquery\' ) );
// make the ajaxurl var available to the above script
wp_localize_script( \'ajax-calls\', \'the_ajax_script\',
array( \'ajaxurl\' => plugin_dir_url(\'my_plugin.php\') ,
\'_ajax_nonce\' => wp_create_nonce( \'my_ajax_nonce\' )
) );
}
add_action(\'wp_print_scripts\', \'ajax_load_scripts\');
而不是试图在我的插件条目文件中捕获发布的数据
function tipps_processing_function(){
check_ajax_referer(\'my_ajax_nonce\');
if ($_POST[\'results\']){
$token = \'393a9276ae329e00b3739d2e76e52f3b\';
$tips = json_encode($_POST[\'results\']);
$save = new Tipspiel();
$save->save_tips($token, $tips);
var_dump($_POST[\'results\']);
return \'OK\';
}
//do stuff here
}
add_action(\'wp_ajax_nopriv_add_tipps\', \'text_ajax_process_request\');
我的js
$j("form#tiepspiel").submit(function(e) {
e.preventDefault(); // this disables the submit button so the user stays on the page
var str = $j(this).serialize();
//alert(decodeURIComponent(str));
if ($j(\'ul#sortables li\').size() > 10){
$j( "#dialog-modal" ).dialog({
height: 140,
modal: true
});
return;
}
var data = {
action: \'add_tipps\',
tips: str,
_ajax_nonce: the_ajax_script._ajax_nonce
};
$j.post(the_ajax_script.ajaxurl, data, function(response) {
alert(\'Got this from the server: \' + response);
//$j.cookie("tipspiel", "competitor", { expires: 1 });
$j(\'form#tiepspiel\').fadeOut(\'slow\', function() {
// Animation complete.
});
});
});
但我没有看到我的varibales被丢弃