这是我的js代码
var geoSearch = new google.maps.Geocoder;
jQuery(document).ready(function() {
var address = FYN_search.postcode;
if(jQuery.type( address ) === "string"){
var region = \'Austria\';
address = address + \', \' + region;
geoSearch.geocode({\'address\':address,\'region\':region},function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var point = results[0].geometry.location;
var data={
action:\'filter_search\',
lat:point.lat(),
lng:point.lng()
}
jQuery.post(FYN_search.ajaxurl, data, function(returndata){
console.log(\'ajax posting\');
jQuery(\'#resulit\').html(returndata);
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
});
此代码将调用名为“filter\\u search”的方法(&;此方法在索引文件中的插件中定义
add_action(\'wp_ajax_filter_search\', \'filter_search_result\');
function filter_search_result(){
echo \'hello world\';
}
但是为什么这个方法没有运行??
最合适的回答,由SO网友:cybmeta 整理而成
wp_ajax_{my-action}
操作挂钩仅用于管理端。对于前端,您必须使用wp_ajax_nopriv_{my-action}
. 如果ajax操作适用于双方,则可以将两者结合起来。例如:
add_action(\'wp_ajax_filter_search\', \'filter_search_result\');
add_action(\'wp_ajax_nopriv_filter_search\', \'filter_search_result\');
另外,不要忘记,永远不要忘记在ajax操作结束时死亡或退出,因为WordPress不会在ajax请求时关闭程序,至少您可以使用
wp_send_json
函数系列。
add_action(\'wp_ajax_filter_search\', \'filter_search_result\');
add_action(\'wp_ajax_nopriv_filter_search\', \'filter_search_result\');
function filter_search_result(){
echo \'hello world\';
exit;
}