Wednesday, 6 June 2012

Jquery ajax call - jQuery.ajax()

Jquery ajax call - jQuery.ajax()

Here is the JQuery asynchronous HTTP (Ajax) request.

$(function () {
$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType

});

});


url : A string containing the URL to which the request is sent.

date: Data to be sent to the server. It is converted to a query string.

success: success(data)
A function to be called if the request succeeds.

dataType: The type of data that you're expecting back from the server.



Here is the example code in mu view file. I am uisng HAML

  :javascript
    $(function () {

    $('#party-schedule').on('show', function () {

      $.ajax({
      url: "#{party_schedules_parties_path}",
      data:'section=party',
       success:function(data){
        $('#party-schedule').html(data);
       },
      dataType:'html'
      });

    })


---------------------------------

The above ajax call will send a request to server as below:

Started GET "/parties/party_schedules?section=party" for 127.0.0.1 at 2012-06-07 11:43:19 +0530
Processing by PartiesController#party_schedules as HTML
  Parameters: {"section"=>"event"}


------------------------------------

So you need to have party_schedules method inside the parties controler to respond for this request.
When request comes to controller, it responds to respective view file.

Note: You should use render :layout => false inside the method.


Thank You,
Uma Mahesh


No comments:

Post a Comment