helloxielan 2017-08-24 10:43 采纳率: 0%
浏览 84

使用ajax获取模板

I am trying to make a customfield search that can be inserted anywhere it should load the results in the same page so i thought ajax would be the trick but it's loading the same page again for some reason here is my search template code:

<script>
    jQuery(document).ready(function($){
        $("#SearchButton").click(function(){
            $.ajax({
                type: "GET",
                ulr:'<?php echo admin_url('admin-ajax.php'); ?>',
                data: { action : 'inline_search', Sinput: $("#searchinput").val() },
                success: function(result) {
                    $("#SearchResults").html(result);
                }
            });
            $("#SearchResults").css("display","block")
        });
    });
</script>
<div class="flexible-content__content">
    <div class="column">
        <div class="row">
            <?php the_sub_field( 'content' ); ?>
        </div>
    </div>
    <div class="column">
        <div class="row">
                <input id="searchinput" type="text" value="<?= $search_query; ?>" name="s" id="header-search-input" placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" style="border-right: none;">
                <bttuon id="SearchButton" class="gform_button button" value="Search">Search</bttuon>
        </div>
    </div>
</div>
<div id="SearchResults" style="max-width: 70rem; margin:auto;display:none"></div>

and i added this to function.php:

add_action( 'admin_enqueue_scripts', 'my_enqueue' );
function my_enqueue($hook) {
    // in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    wp_localize_script( 'ajax-script', 'ajax_object',
        array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
}

function inline_search()
{
    echo "hello world";
    get_template_part('templates/inline-search');
    wp_die();
}
add_action('wp_ajax_nopriv_inline_search', 'inline_search');
add_action('wp_ajax_inline_search', 'inline_search');
  • 写回答

2条回答 默认 最新

  • ℡Wang Yan 2017-08-24 11:02
    关注

    The PHP part would be like this.

    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    function my_enqueue($hook) {
        // in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
        wp_register_script('ajax-script', SCRIPT_PATH, array('jquery'));
        wp_localize_script( 'ajax-script', 'ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
        wp_enqueue_script('ajax-script');
    }
    
    function inline_search()
    {
        echo "hello world";
        get_template_part('templates/inline-search');
        wp_die();
    }
    add_action('wp_ajax_nopriv_inline_search', 'inline_search');
    add_action('wp_ajax_inline_search', 'inline_search');
    

    Then in js, you have access to ajax_object like ajax_object.we_value. Use ajax type POST.

    $.ajax({
                    type: "POST",
                    url: ajax_object.ajax_url,
                    data: { action : 'inline_search', Sinput: $("#searchinput").val() },
                    success: function(result) {
                        $("#SearchResults").html(result);
                    }
                });
    
    评论
  • 狐狸.fox 2017-08-24 12:31
    关注

    Thanks for @mokiSRB to point it out but the whole problem was a a typo i feed so stupid.

    changing ulr: to url fixed it and it works now.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#git#的问题,请各位专家解答!(相关搜索:开源项目)
  • ¥15 matlab处理脑电数据悬赏(时序图+预处理+频谱图)
  • ¥100 r语言多元回归模型怎么看表达式
  • ¥20 jenkins+sonarqube实现代码门禁时遇到的问题
  • ¥15 PMM 9010/30P
  • ¥15 pom文件依赖管理,未找到依赖
  • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
  • ¥15 Unity导出2D项目运行时图片变成马赛克
  • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
  • ¥15 matlab中此类型的变量不支持使用点进行索引