I am trying to figure out how to send a post request to a WP Rest API function on an AMP Page.
The endpoint requires 2 fields, post_id & nonce.
/wp-json/wordpress-popular-posts/v2/views/[post_id] as per the guide on https://github.com/cabrerahector/wordpress-popular-posts/wiki/8.-REST-API-Endpoints
Now the task here is how to send a request from the AMP page to this endpoint. My theme offers the ability to add custom html/javascript/CSS code to the AMP page.
But how can we get the nonce and post_id dynamically using Javascript?
Can the below code help me solve the issue?
$.ajax({ url: foo, method: "POST", beforeSend: function(xhr) { xhr.setRequestHeader("X-WP-Nonce", wpApiSettings.nonce); }, data: { "foo": bar } }).done(function(response) { // your custom code });