栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Wordpress使用Wordpress将Ajax值传递到特定页面

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Wordpress使用Wordpress将Ajax值传递到特定页面

您距离很近,但缺少一些小东西……

我在评论中的意思是,您需要

'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
$_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 oneadd_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );

对于您的jQuery脚本

script.js
文件,有2件重要的遗漏小事情:

jQuery(document).ready(function($) {        var fruit = 'Banana';        $.ajax({        url: ajax_object.ajaxurl,         type : 'post',         data: { 'action':'example_ajax_request', 'fruit' : fruit        },        success:function(data) {  console.log(data);        },        error: function(errorThrown){ console.log(errorThrown);        }    });});

现在应该可以工作了……

参考文献:

  • 在没有插件的情况下在WordPress网站上将AJAX与PHP结合使用

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/436878.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号