ajax to post and return html to display in div

Brad picture Brad · Feb 5, 2013 · Viewed 11.8k times · Source

jQuery v 1.9.0

When the page loads I am firing two functions (get_month() & get_todays_events()) and those return the calendar and today's events html.

Sometimes when I refresh the page, it will display the today's events twice and does not display the calendar(month), as seen below.

enter image description here Half the time it works just fine, as seen below:

enter image description here

There are no errors in the firebug console. It works fine as a stand-alone app, but when I incorporate it into Jasig's uPortal, it messes up.

Then I tried stripping the html that gets sent back from the ajax posts and I noticed the get_todays_events(); may be the culprit, but I still don't know what it could be.

Is there something in the html that may be tripping things up or should I have a delay on firing that function after the get_month(); fires?

$.noConflict();
jQuery(document).ready(function($) {

  function get_month()
  {
    var url_month = jQuery("#days_for_month_url").attr("href");

    var form_data = { ajax : '1' };

    jQuery.ajax({
      type: "POST",
      dataType: "html",
      url: url_month,
      crossDomain: true,
      data: form_data,
      success: function( default_month_msg ) {

        jQuery('#display-month').html(default_month_msg);

      } // end success

    }); // end ajax
    //return false;
  }

  function get_todays_events()
  {
    var url_todays_events = jQuery("#todays_events_url").attr("href");

    var form_data = { ajax : '1' };

    jQuery.ajax({
      type: "POST",
      dataType: "html",
      url: url_todays_events,
      crossDomain: true,
      data: form_data,
      success: function( default_month_msg ) {

        jQuery('#display_todays_events').html(default_month_msg);

      } // end success

    }); // end ajax
    //return false;
  }

  get_month();
  console.log("display calendar");

  get_todays_events();
  console.log("display today's events");

  jQuery(document).on('click', "a.view_events", function(e) {
    e.preventDefault();
    var date = jQuery(this).data('date');

    // change if URL changes
    var url_events = jQuery("#events_for_day_url").attr("href");

    var form_data = {
      year : jQuery(this).data('year'),
      month : jQuery(this).data('month'),
      day : jQuery(this).data('day'),
      date : jQuery(this).data('date'),
      ajax : '1'
    };

    jQuery.ajax({
      type: 'POST',
      dataType: 'html',
      url: url_events,
      data: form_data,
      success: function( msg ) {

        jQuery('#display-events').html(msg);
        jQuery('#display-event_detail').html('');
        jQuery('#display_todays_events').html('');

      } // end success

    }); // end ajax

    return false;

  }); // end on.click view-events

  jQuery(document).on('click', "a.view_event_details", function(e) {
    e.preventDefault();
    var url_event_detail = jQuery("#event_detail_url").attr("href");

    var form_data = {
      event_id : jQuery(this).data('event_id'),
      ajax : '1'
    };

    jQuery.ajax({
      type: 'POST',
      dataType: 'html',
      url: url_event_detail,
      data: form_data,
      success: function( msg ) {

        jQuery('#display-event_detail').html(msg);
        jQuery('#display-events').html('');
        jQuery('#display_todays_events').html('');

      } // end success

    }); // end ajax

    return false;

  }); // end on.click view_event_details

  jQuery(document).on('click', "a.month_link", function(e) {
    e.preventDefault();

    jQuery('#display-events').html('');

    var url_days_for_month = jQuery("#days_for_month_url").attr("href");

    var form_data = {
      month_url : jQuery(this).data('month_url'),
      ajax : '1'
    };

    jQuery.ajax({
      type: 'POST',
      dataType: 'html',
      url: url_days_for_month,
      data: form_data,
      success: function( msg ) {

        jQuery('#display-month').html(msg);
        jQuery('#display_todays_events').html('');

      } // end success

    }); // end ajax

    return false;

  }); // end on.click view-month

  jQuery(document).on('click', "a.view_subscriptions", function(e) {
    e.preventDefault();
    var url_subscription = jQuery("#subscriptions_url").attr("href");

    var form_data = {
      ajax : '1'
    };

    jQuery.ajax({
      type: 'POST',
      dataType: 'html',
      url: url_subscription,
      data: form_data,
      success: function( msg ) {

        jQuery('#display-subscriptions').html(msg);
        //jQuery('#display-subscriptions').toggle();

      } // end success

    }); // end ajax

    return false;

  }); // end on.click view-subscriptions

  jQuery(document).on('click', "#update_subscriptions", function(e) {
    e.preventDefault();
    var url_update_subscription = jQuery("#update_subscriptions_url").attr("href");

    var vals = []
    jQuery('input:checkbox[name="calendar[]"]').each(function() {
        if (this.checked) {
            vals.push(this.value);
        }
    });

    var form_data = {
      ajax : '1',
      calendar: vals,
    };

    jQuery.ajax({
      type: 'POST',
      dataType: 'html',
      url: url_update_subscription,
      data: form_data,
      success: function( msg ) {

        get_month();
        jQuery('#display-subscriptions-update').html(msg).fadeOut(6000);
        jQuery('#display-subscriptions').html('');
        jQuery('#display-events').html('');
        jQuery('#display_todays_events').html('');

      } // end success

    }); // end ajax

    return false;

  }); // end on.click view-subscriptions

}); // end document ready

Here is the html that is returned on success for the get_todays_events() function

<h3>Today's Events</h3>


  <p><a href="#" class="view_event_details" data-event_id="68">All About Heroin</a> - 12:30 pm</p>


  <p><a href="#" class="view_event_details" data-event_id="67">African Storyteller - Ekow King</a> - 12:30 pm</p>


  <p><a href="#" class="view_event_details" data-event_id="66">Men&rsquo;s Basketball vs. Columbia-Greene Community College</a> - 6:00 pm</p>

UPDATE: checked the net tab in firebug and examined XHR, when using in uPortal, it would fire off two post requests and two get requests. Both POST requests were retuning 0 byte, but the GET requests were returning something. I changed them to GETS, since I wasn't really asking the user to enter data, but click a link that had the data already (that being the date they wanted view events for), so I appended the date value to the URL I was passing into the ajax get request, then it returned it correctly every time.

Answer

Ron Fridman picture Ron Fridman · Apr 4, 2013

Try to add to the ajax call parameters async=false (add this to all the ajax calls).

For example:

 jQuery.ajax({
  type: "POST",
  dataType: "html",
  url: url_todays_events,
  crossDomain: true,
  data: form_data,
  async:false,
  success: function( default_month_msg ) {

    jQuery('#display_todays_events').html(default_month_msg);

  } // end success

});