admin管理员组文章数量:1389897
I have a simple click function that gets the data of a radio button in a form and passes it to a function in my functions.php file. The problem is the Ajax never calls my action in my functions.php file. I always get "nothing found" as set in my ajax function.
Here is my code below, if anyone can spot the problem would be greatly appreciated.
JS:
$(".faq-category-select").on("click",function() {
var faqCategory = $(this).attr('value');
var str = '&faq_category=' + faqCategory + '&action=load_faqs_by_category';
$.ajax({
type: "POST",
dataType: "html",
url: ajax_params.ajaxurl,
data: str,
success: function(data) {
var $data = $(data);
if($data.length){
$("#faq-archive-content-wrap").append($data);
} else{
$("#faq-archive-content-wrap").append('<p>nothing found</p>');
}
},
error : function(jqXHR, textStatus, errorThrown) {
$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
return false;
});
HTML:
<form action="" id="faq-filter-form" method="post">
<h3>Heading 1</h3>
<input type="radio" name="faq-category-select" class="faq-category-select" value="value 1"><label>Value 1</label>
<input type="radio" name="faq-category-select" class="faq-category-select" value="Value 2"><label>Value 2</label>
<h3>Heading 2</h3>
<input type="radio" name="faq-category-select" class="faq-category-select" value="value 3"><label>Value 3</label>
<input type="radio" name="faq-category-select" class="faq-category-select" value="value 4"><label>value 4</label>
</form>
functions.php
add_action ( 'wp_ajax_load_faqs_by_category', 'load_faqs_by_category' );
add_action ( 'wp_ajax_nopriv_load_faqs_by_category', 'load_faqs_by_category' );
function load_faqs_by_category() {
$faq_category = (isset($_POST['faq_category'])) ? $_POST['faq_category'] : '';
header("Content-Type: text/html");
$output = '';
$output .= 'you selected the'.$faq_category.'category';
die($output);
}
wp_register_script( 'faq-ajax', get_template_directory_uri() . '/js/faq-ajax.js', '','',true );
wp_localize_script( 'faq-ajax', 'ajax_params', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
wp_enqueue_script( 'faq-ajax' );
本文标签: WP Ajax never returning any datacalling action
版权声明:本文标题:WP Ajax never returning any datacalling action 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744621972a2616083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论