Blessan Kurien
Blessan Kurien

Reputation: 1665

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??

Upvotes: 1

Views: 175

Answers (2)

Jai
Jai

Reputation: 74738

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
    }

Upvotes: 4

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

Write success and error within settings variable itself:

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
        },
        "success":function(resp){
            //your work here
        },
        "error":function(resp){
            //your work here
        }
      }


$.ajax(settings);

Or remove it from settings and bind it globally to document as

$(document).ajaxSuccess(function( event, xhr) {
    //error code here
}).ajaxError(function(event,xhr){
   //error code here
});

Upvotes: 0

Related Questions