0

Here's my code :

var request = $.ajax({
    url: 'some.url'    
});
request.done(function(result){
    console.log(result);
});
request.fail(function(result){
    console.log(result);
});

My question is: Is there any way to initiate the same ajax call with just the variable name (In this case it's request) ???

Like: request.SomeMethodToCallSameAjax();

chridam
  • 95,056
  • 21
  • 214
  • 219
Jalay
  • 66
  • 1
  • 7

1 Answers1

1

You can create a function:

var myAjax = function() {
  return request = $.ajax({
     url: 'some.url'    
  });
}

myAjax.done(function(result) { console.log(result); }
myAjax.fail(function(result) { console.log(result); }

When you need to:

//Call my function
myAjax();
alexmngn
  • 8,371
  • 16
  • 65
  • 127
  • Thanks :) That's what I wanted. But I think `myAjax.done()` wont work. Instead there should be `myAjax().done()` – Jalay May 05 '14 at 06:20