Wednesday, 18 April 2012

jQuery missing : after property id


Hi,

I am sending ajax request from a webpage and getting error in my error console as 'after property id'

Here is my code

  $('#link').click(function(){
                  $.ajax({
                  type : 'get',
                  url : '/myurl',
                  datatype : 'json',
                  async: false,
                  beforeSend: {
                        $("#spinner").show();
                  },
                  headers: {
                       'X-Transaction': 'POST Example',
                       'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                     },
                  success : function(data) {
                  $("#spinner").hide();

                  },
                  error : function(data){
                  $("#spinner").hide();
                  }
                });
       });


after googling I came to see its an issue with the 'beforeSend' parameter.

beforeSend expects a function. You are trying to pass it a pseudo object-literal having an invalid syntax.

Here is the correct syntax:

                  $('#link').click(function(){
                                  $.ajax({
                                  type : 'get',
                                  url : '/myurl',
                                  datatype : 'json',
                                  async: false,
                                  beforeSend: function() {
                                        $("#spinner").show();
                                  },
                                  headers: {
                                       'X-Transaction': 'POST Example',
                                       'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')
                                     },
                                  success : function(data) {
                                  $("#spinner").hide();

                                  },
                                  error : function(data){
                                  $("#spinner").hide();
                                  }
                                });
                       });

Now the spinner image is displaying fine.

Thank You,
Uma Mahesh.

No comments:

Post a Comment