Blessan Kurien Blessan Kurien - 6 months ago 11
Ajax Question

Bad request:Error function and timeout in ajax never fired

Here is my ajax call

var settings = {
"async": true,
"crossDomain": true,
"url": "http://sample.com/customer/api/v1/meetings/meeting",
"method": "POST",
"headers": {
"x-api-key": token
},
"dataType":'json',
"data": {
"title": meetingData.title,
"end_date": meetingData.endtdate,
"from_date": meetingData.startdate,
"description": meetingData.description,
"reminder_type": meetingData.remainder,
"priority": meetingData.priority
}
}

$.ajax(settings).done(function (response) {
console.log(response);
},function(err){
alert('error');
});


Problem is error callback function is never fired if any error is present.
Also i want to add a timeout this ajax call ,how can i do that??
I've added
timeout:20000
parameter in my
settings
variable but the error call back is not fired

UPDATE

I will get a bad request error from the console ,how to solve that error??

Jai Jai
Answer

I doubt the .done() has a error callback, instead you should use .then(successCB, errorCB) which has both callbacks:

$.ajax(settings).then(function (response) {
    console.log(response);
},function(err){
   alert('error');
});

Update

As per updates: I will get a bad request error from the console ,how to solve that error??

Then in this case the doubtful entities could be your data object you are sending, one of them or more might have some data which is causing this error. So you can inspect what data is getting through here:

"data": {
      "title": meetingData.title,
      "end_date": meetingData.endtdate,
      "from_date":  meetingData.startdate,
      "description": meetingData.description,
      "reminder_type": meetingData.remainder,
      "priority": meetingData.priority
    }