我正在嘗試從 wordpress ajax 獲得回應。但出現 400 錯誤。找不到問題出在哪里。我已經嘗試了很多次,但無法解決。jQuery 版本 3.6。filter.js 檔案運行良好。請幫我。
函式.php
add_action( 'init', 'my_script_enqueuer' );
function my_script_enqueuer() {
wp_register_script( "filter_script", get_template_directory_uri() . '/assets/js/filter.js', array('jquery') );
wp_localize_script( 'filter_script', 'filterAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'filter_script' );
}
add_action("wp_ajax_publication_filter_function]", "publication_filter_function");
add_action("wp_ajax_nopriv_publication_filter_function", "publication_filter_function");
function publication_filter_function(){
echo "Response hello";
}
過濾器.js
jQuery(document).ready( function() {
// Get filter value
jQuery('.publication-filter').on('change', function(e){
e.preventDefault();
var filter_value = this.value;
var nonce_val = jQuery('.filter_ajax_nonce').val();
jQuery.ajax({
type : "POST",
url : filterAjax.ajaxurl,
data : {
action: "publication_filter_function",
filter_value : filter_value,
nonce: nonce_val
},
success: function(response) {
if(response.type == "success") {
console.log("Hello");
}
else {
console.log("Not hello");
}
},
error : function(error){ console.log(error) }
});
})
});
回復

uj5u.com熱心網友回復:
您需要更改此行:
add_action("wp_ajax_publication_filter_function", "publication_filter_function");
我認為您]錯誤地將括號添加到了 ajax 掛鉤中,這就是 ajax 呼叫不起作用的原因。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537089.html
標籤:javascriptPHP查询阿贾克斯WordPress的
