I recently implemented a wp_localize_script
for ajax in my WordPress project:
wp_localize_script('cb_admin_js', 'cbAjax', array('ajax_url' => admin_url( 'admin-ajax.php' )));
As part of testing, I used an $http.get()
in Angular:
$http.get('http://localhost/wp-admin/admin-ajax.php',{action:'get_saved_cover'}).then(function(data){
console.log(data);
});
The goal of my function was to retrieve and return a list of cars:
add_action( 'wp_ajax_nopriv_get_saved_cover', 'get_saved_cover' );
add_action( 'wp_ajax_get_saved_cover', 'get_saved_cover' );
function get_saved_cover(){
$cars=array("Volvo","BMW","Toyota");
return json_encode($cars);
wp_die();
}
However, when I make the request, all I receive back is data: 0
, status: 200
, and some other standard responses in an array. It seems like there might be a missing piece that Angular requires or something else causing the issue.