AJAX to RESTfully send and save data

‘endpoint’

= url where we can get and save data
/——a—–/———–b-=——-/-c/—d—-
/wp-json/cupcake-voting/v1/votes

where
a is required by the REST plugin
b is the unique string for the endpoint
bc is the namespace
d is the route (the specific function for sending or receiving data)

There are http standards for sending or receiving data
GET for reading data
PUT for updating existing data
POST for adding new data
DELETE for deleting existing data

Registering an endpoint

(js)
The call:
register-rest-route(
'cupcake-voting/v1' ,
'/votes/' ,
array(
'methods' => 'POST' ,
'callback' => 'cupcake_add_vote',
)
);

NEXT wrap the js call in a function and use an action to register that function via the rest_api_init function
function cupcake_activate(){
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'cupcake_activate' );
function cupcake_register_endpoint(){
register-rest-route(
'cupcake-voting/v1' ,
'/votes/' ,
array(
'methods' => 'POST' ,
'callback' => 'cupcake_add_vote',
)
); }
add_action( 'rest_api_init', 'cupcake_register_endpoint);
function cupcake_add_vote( WP_REST_REQUEST $request ){
return $request->get_params();
}