Philipp Grigoryev
Philipp Grigoryev

Reputation: 2118

jquery ajax .done is not fired but .always is fired

I'm trying to attach my jquery code to the 3rd party site, so I'm inserting a button which triggers a request when a page is loaded. An issue I have is that .done callback is not triggered, but .always is got triggered. The only one thing worth to mention is that the page, I try to work with, also makes some ajax request after it's loaded. Could it be a reason why I have the issue?

$("<button>Test</button>").click(function(){
  var url="<https url from the site>";
  $.ajax(url).done(function(data){
    console.log(data);
  });
}).insertBefore("div.top");

Upvotes: 0

Views: 4905

Answers (2)

Philipp Grigoryev
Philipp Grigoryev

Reputation: 2118

I was able to solve the issue with a help from Arun.

There was an error while parsing a response. So after i added an additional parameter {dataType : "json"} to the request, .done is triggered. It looks now like

$.ajax(url,{
    dataType: "json"
}).done(function(data){
    console.log("Success:" + data);
}).fail(function(xhr, status, error){
    console.log("Status: " + status + " Error: " + error);
    console.log(xhr);
});

Upvotes: 2

ianaya89
ianaya89

Reputation: 4233

Your done function will only be executed if your ajax call finish successfully, if something went wrong done will never get triggered. As its name indicates, the always method will always get triggered regardless your ajax request works or not.

Upvotes: 0

Related Questions