How can I get the values of my WordPress $wpdb query in Jquery?

I have this function to grab the zip codes from WooCommerce

public function validate_zipcode () {

global $wpdb, $woocommerce;

$sql = ‘ SELECT location_code FROM ‘ . $wpdb->prefix. ‘woocommerce_shipping_zone_locations ‘;
$sql .= “WHERE location_code IS NOT NULL”;

$zones = $wpdb->get_results($sql);

return $zones;

}

add_action( ‘wp_ajax_nopriv_validate_zipcode’, array( &$this, ‘validate_zipcode’ ));

I would like to use Jquery to get the results but I am unable to, the error response is a bad request, what am I doing wrong?

jQuery(“.check-zip”).click(function(){

var zipCode = jQuery(“#custom_zip”).val();
var zipArray = [];

jQuery.ajax({
type: ‘GET’,
url: ajaxurl,
data: {
action: ‘validate_zipcode’
},

success: function(data) {
console.log( data );

},

error: function( jqXHR, textStatus, errorThrown ) {

console.log( jqXHR, textStatus, errorThrown );
}
});

Read more here:: How can I get the values of my WordPress $wpdb query in Jquery?

Leave a Reply

Your email address will not be published. Required fields are marked *