dongmie3526 2015-07-02 12:26
浏览 78

如何在自定义页面中使用ajax并在wordpress中发布

I have created a custom pages whit country and city dropdown. I want to use ajax to select city to according to the country selection. I tried to add ajax call directly in my page but it doesnt work

I dont know how to fix it.

Please help me, your help will appreciated heartly.

my link is as under my site

  • 写回答

1条回答 默认 最新

  • dongpaocuan7498 2015-07-02 12:50
    关注

    You need to use wp_ajax_(action) hook in order to use ajax in your pages.

    <?php 
    
        add_action( 'wp_ajax_add_foobar', 'prefix_ajax_add_foobar' );
        add_action( 'wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar' );
    
        function prefix_ajax_add_foobar() {
            // Handle request then generate response using WP_Ajax_Response
        }
    ?>
    <script>
    
        jQuery.post(
            '<?php echo admin_url('admin-ajax.php') ?>', 
            {
                'action': 'add_foobar',
                'data':   'foobarid'
            }, 
            function(response){
                alert('The server responded: ' + response);
            }
        );
        </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)