有谁能在这方面帮助我吗。我的问题是,当我ajax请求WordPress时,当我在控制台中记录响应时,它会显示error/complete
不response
.
jQuery(document).ready(function($) {
$(".pincode_search_field").on(\'keyup\', function(event) {
event.preventDefault();
var ajax_url = pin_shop.ajax_url;
var pincode_search_field = $(this).val();
var nonce = $(this).data(\'nonce\');
$.ajax({
url: ajax_url,
type: \'POST\',
data: {
\'action\' : \'pincode_search_field\',
\'nonce\' : nonce,
\'search_query\' : pincode_search_field,
},
})
.done(function(response) {
console.log(response.database_result);
console.log(\'success\');
})
.fail(function() {
console.log("error");
});
});
});这是我的PHP代码。
add_action( "wp_ajax_pincode_search_field", \'pincode_search_field\' );
add_action( "wp_ajax_nopriv_pincode_search_field", \'pincode_search_field\' );
function pincode_search_field() {
global $wpdb;
$pincode = $_REQUEST[\'search_query\'];
if ( wp_verify_nonce( $_REQUEST[\'nonce\'], "pin_shop_nonce") ) {
$result[\'database_result\'] = $wpdb->get_results("SELECT shop_id FROM cs_shop_details WHERE shop_pincode = $pincode");
$result = json_encode( $result );
echo $result;
die();
}
}
这是排队脚本。add_action( \'wp_enqueue_scripts\', function(){
wp_enqueue_script( \'pin-js\', plugins_url() . \'/pin-shop/inc/pin.js\', array(\'jquery\') );
wp_localize_script( \'pin-js\', \'pin_shop\', array(
\'ajax_url\' => admin_url(\'admin-ajax.php\'),
) );
});
编辑:这是控制台日志中的结果。