Mampel
Mampel

Reputation: 13

no response from ajax in when method

i have no response after call ajax method in $.when:

$.when(tht.ajax.getUserDrp()).done(function (data) {
    if(data)  console.log('hava data')
    else  console.log('no data')
});

getUserDrp function:

getUserDrp: function(){
  $.ajax({
    url: "../account/getLevel" ,
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
       // do somthing
    }
  });
}

as a result always print 'no data' in console, why?

Upvotes: 1

Views: 35

Answers (2)

To use when, done, you need to change your function as follows

getUserDrp: function(){
  return $.ajax({
    url: "../account/getLevel" ,
    type: "POST",
    contentType: "application/json; charset=utf-8"
  });
}

You could also use a callback instead

getUserDrp: function(callback){
  $.ajax({
    url: "../account/getLevel" ,
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
       callback(data)
    }
  });
}

getUserDrp(function(data){
if(data)  console.log('hava data')
    else  console.log('no data')
})

Upvotes: 0

GiamPy
GiamPy

Reputation: 3560

You are trying to use getUserDrp as a promise but you don't return the $.ajax function result, that's why .done() never gets called.

Try:

getUserDrp: function(){
  return $.ajax({
    url: "../account/getLevel" ,
    type: "POST",
    contentType: "application/json; charset=utf-8"
  });
}

Upvotes: 2

Related Questions