WordPress使用WordPress将ajax值传递给特定页面

我想将变量传递给特定页面。 我找到了一个简单的例子,解释了如何在wordpress中使用ajax。

JavaScript的:

jQuery(document).ready(function($) { // We'll pass this variable to the PHP function example_ajax_request var fruit = 'Banana'; // This does the ajax request $.ajax({ url: ajaxurl, data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); }); 

要在functions.php插入的PHP片段

 function example_ajax_request() { // The $_REQUEST contains all the data sent via ajax if ( isset($_REQUEST) ) { $fruit = $_REQUEST['fruit']; // Let's take the data that was sent and do something with it if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now we'll return it to the javascript function // Anything outputted will be returned in the response echo $fruit; // If you're debugging, it might be useful to see what was sent in the $_REQUEST // print_r($_REQUEST); } // Always die in functions echoing ajax content die(); } add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); 

不幸的是我无法传递变量。 我检查了代码,我收到了这个错误:

 Error: ajax_object is not defined 

您是否知道另一种获得相同结果的方法?

你很近,但有一些小事丢失了……

我在评论中的意思是,你需要在两者中使用'ajax-script'这样使用它:

 add_action('wp_enqueue_scripts', 'add_js_scripts'); add_js_scripts(){ wp_enqueue_script( 'ajax-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true ); wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } 

$_REQUEST更改$_REQUEST $_POST

 function example_ajax_request() { // The $_REQUEST contains all the data sent via ajax if ( isset($_POST) ) { $fruit = $_POST['fruit']; // Let's take the data that was sent and do something with it if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now we'll return it to the javascript function // Anything outputted will be returned in the response echo $fruit; // If you're debugging, it might be useful to see what was sent in the $_POST // print_r($_POST); } // Always die in functions echoing ajax content die(); } 

添加了add_action( 'wp_ajax_nopriv_ … )

 add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' ); // <= this one add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); 

对于你的jQuery脚本script.js文件,有两件重要的遗漏:

 jQuery(document).ready(function($) { /* We'll pass this variable to the PHP function example_ajax_request */ var fruit = 'Banana'; /* This does the ajax request */ $.ajax({ url: ajax_object.ajaxurl, /* <====== missing here */ type : 'post', /* <========== and missing here */ data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { /* This outputs the result of the ajax request */ console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); }); 

这应该现在起作用......

参考文献:

  • 在没有插件的情况下在WordPress站点上使用AJAX和PHP

  • 如何在您的WordPress插件或主题中使用Ajax?

您正在使用wp_localize_script错误。 在PHP代码中,删除wp_localize_script行。

您可以(并且应该)添加以下内容

 // this line is for users who are not logged in add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' ); 

一切都是正确的,除非您必须在url:$ .ajax函数的参数中添加ajax_object.ajax_url而不是ajaxurl

 jQuery(document).ready(function($) { // We'll pass this variable to the PHP function example_ajax_request var fruit = 'Banana'; // This does the ajax request $.ajax({ url: **ajax_object.ajax_url**, data: { 'action':'example_ajax_request', 'fruit' : fruit }, success:function(data) { // This outputs the result of the ajax request console.log(data); }, error: function(errorThrown){ console.log(errorThrown); } }); }); 

复制并粘贴上面的脚本代替你的$ .ajax函数如果你想看看什么是ajax_object.ajax_url然后警告它inisde你的代码,它将提醒你的管理ajax文件的路径,这是在wordpress中使用ajax所必需的