I try to get my first AJAX Function working in Wordpress 4.3.
I have build an plugin named "calipso". In the plugin there are the two files
calipso.php :
<?php
/**
* @package calipso
* @version 1.0
*/
/*
Plugin Name: calipso
Plugin URI: http://www.calipso.de
Description: Dieses Plugin for Calipso-Projekt.
Author: Calipso
Version: 1.0
Author URI: http://www.calipso.de
*/
function llv_integrates() {
$plugin_url = plugins_url( '/', __FILE__ );
wp_enqueue_script('MyAjax',$plugin_url . 'MYajax.js', array( 'jquery' ),'1.0.0',false);
$ajaxObjekt = array( 'ajaxURL' => admin_url( 'admin-ajax.php' ) );
wp_localize_script('MyAjax', 'ajaxObjekt', $ajaxObjekt);
}
add_action( 'wp_enqueue_scripts', 'llv_integrates' );
function TEST_callback(){
$anzahl= $_POST['anzahl'];
//$anzahl = isset($_POST['anzahl']) ? $_POST['anzahl'] : '';
$datei_handle=fopen("logmeInWPAnzahl.txt","a");
fwrite($datei_handle, "Anzahl: ".$anzahl."\n");
fclose($datei_handle);
wp_die();
}
add_action('wp_ajax_TEST_callback','TEST_callback');
add_action('wp_ajax_nopriv_TEST_callback','TEST_callback');
and MYajax.js :
function JStoPHP(){
console.log("JStoPHP is called");
console.log(ajaxObjekt.ajaxURL);
jQuery.ajax({
url:ajaxObjekt.ajaxURL,
data: {action:'TEST_callback' , anzahl: "12315"},
datatype: "json",
type: "POST",
success: function(respose) {
console.log(respose);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
}
JStoPHP();
Expected operation: transfer of the variable 'Number' from the JavaScript function JStoPHP to via ajax called PHP function TEST_callback.
Currently the TEST_callback function seems to be not invoked. Where is my mistake?
该代码必须位于插件中的第一位才能正常工作。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.