I am using the Wordpress heartbeat and am attempting to pass some data to my site using Ajax. I am able to see the action in my $_POST but my data is not available. The below .post is able to send a request to MyZabbix, that post contains my action, my action then works. However, I need access to the indexs['hosted']. Why would the below .post not send my data when sending the action?
$( document ).on( 'heartbeat-tick', function( e, data ) {
$.each( data, function( index, value ) {
$.each( value, function( indexs, values ) {
var hostids = indexs['hostid'];
$.post( MyZabbix.ajaxurl, {
action:'zabbix_ajax_submit',
data: indexs['hostid'] },
function( api_response ) {
console.log( "Data Saved: " + api_response );
});
}(jQuery));
I decided to use the when function with my ajax request. The issue that I was experiencing was - My ajax request didn't always finish before the rest of my code executed. By moving my code into a function that executed only AFTER my ajax request was successful, I was then able to get every thing to behave as expected.
var promise = $.ajax({
type: "POST",
url: MyZabbix.ajaxurl,
async: true,
data: { action:'zabbix_ajax_submit', data:value}
});
$.when(promise).done( successFunction );
The when function is located in the documentation.
The done function is located in the documentation.