为什么AJAX $.post不能在wordpress中工作

Why isn't AJAX $.post working in wordpress?

本文关键字:wordpress 工作 不能 AJAX post 为什么      更新时间:2023-09-26

我一直在试图理解为什么$.post不起作用。我正在尝试进行两次下降..

第一个

下拉列表将包含主类别,第二个下拉列表包含第一个下拉列表的子类别。

[MAIN CATEGORY DROP DOWN] [SUB CATEGORY DROP DOWN]

第一个下拉菜单完美无缺,但由于我不知道的某种原因,子类别无法使用 ajax 获取。

add_shortcode( 'custom_query', 'custom_query' );
add_action( 'wp_ajax_custom_query', 'getSubCategory' );
add_action( 'wp_ajax_nopriv_custom_query', 'getSubCategory' );
function custom_query( $cat_id )
{       
    // first dropdown
    $first_dropdown = array( 'show_option_all'    => '','show_option_none'   => 'Select main category','orderby' => 'ID','order' => 'ASC','show_count' => 0,    'hide_empty' => 0, 'exclude' => 0,'echo' => 1,'selected' => 0,'child_of' => $cat_id,'hierarchical' => 1,'name' => 'chained-categories','id' => '','class' => 'postform',    'depth' => 1, 'tab_index' => 0,'taxonomy' => 'category', 'hide_if_empty' => false ); 
    wp_dropdown_categories( $first_dropdown );
    // subcategories empty but will be populated once the first main category is selected
    echo '<div id="chained-subcontainer">
            <select name="chained-subcategories" id="chained-subcategories">
                <option value="">- Select a category first -</option>
            </select>
        </div>';
    // javascript
    echo '  
        <script type="text/javascript">
            (function($)
            {
                var ajaxurl = "../../wp-admin/admin-ajax.php";
                var subdata = { action: "custom_query" };
                $("#chained-categories").change(function()
                {
                    subdata[ "chained_subcat_id" ] = $( this ).val();
                    subdata[ "chained_subcat_name" ] = $("#chained-categories option[value="+ $( this ).val() + "]").text();
                    if( $( this ).val() > 0 )
                    {
                        #this here works once the first cat is picked
                        alert($("#chained-categories option:selected").val());
                        $.post
                        (
                            ajaxurl, 
                            subdata,   
                            function( response )
                            {
                                $( "#chained-subcontainer" ).html( response );
                            }
                        );
                    } else {
                        $( "#chained-subcontainer" ).html( "<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>" );
                    }   
                });
            })(jQuery);
        </script>
    ';
}
function getSubCategory()
{
    $second_dropdown = array('show_option_all' => '', 'show_option_none' => 'Select subcategory','orderby' => 'ID', 'order' => 'ASC', 'show_count'  => 0, 'hide_empty' => 0, 'exclude' => 0, 'echo' => 1, 'selected' => 0, 'child_of' => $_POST[ 'chained_subcat_id' ], 'hierarchical' => 1, 'name'    => 'chained-subcontainer', 'id' => '', 'class'   => 'postform','depth' => 1, 'tab_index' => 0, 'taxonomy' => 'category', 'hide_if_empty' => false ); 
    wp_dropdown_categories( $second_dropdown );
}
我知道一个事实,它是

$.post部分不起作用,因为我包含一个警报,一旦在第一个下拉列表中选择了任何选项,它就会显示它的值..并且只要不包含代码的$.post部分,它就可以工作。

编辑:所以在大卫的建议之后,我能够检查一些代码,而一切都像预期的那样工作..就ajax工作而言。我有一个小问题..

在这一行代码中,

else {
    $("#chained-subcontainer").html("<select name="chained-subcategories" id="chained-subcategories"><option value="">Select a category first</option></select>");
}  
如果我保持打开状态,代码就ajax而言不起作用..

如果我删除该部分,那么ajax会正确获取数据..我似乎无法理解为什么..这是我从JSHINT得到的" Expected ')' and instead saw 'chained'.

如果我保持打开状态,代码就无法像 ajax 那样工作

那是因为你有一个语法错误,所以没有一个JavaScript可以工作。

请注意堆栈溢出上此处突出显示的语法:

$( "#chained-subcontainer" ).html( "<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>" );
                                                 ^--- here

在这种情况下,最简单的解决方案就是对字符串使用单引号。 由于您的字符串本身不包含单引号(但不要使用双引号):

$( '#chained-subcontainer' ).html( '<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>' );

每当字符串包含引号字符时,都必须确保解析器不会将该字符解释为字符串的末尾。 通过使用不同类型的引号或"转义"这些引号字符。