weixin_33671935 2015-03-24 21:39 采纳率: 0%
浏览 64

jQuery .post不发送数据

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));
  • 写回答

1条回答 默认 最新

  • weixin_33674437 2015-03-25 22:11
    关注

    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.

    评论

报告相同问题?