0

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"); 
6
  • that means the ajax request failed for some reason... try to have a fail handler and see what is the reason for failure Commented Nov 20, 2014 at 4:55
  • $.ajax(url).done(function (data) { console.log(data); }).fail(function (jqXHR, status, error) { console.log(jqXHR, status, error) }); Commented Nov 20, 2014 at 4:56
  • @ArunPJohny instead of manually typing all of those arguments, a simple console.log(arguments) can save you debugging time! Probably about 5 seconds per argument set, 5*2 = 10 seconds of time. Commented Nov 20, 2014 at 5:02
  • @self yes... but I just wanted to be specific and show what are the arguments to OP... also I have a code snippet saved for the fail handler which I copy paste in SO Commented Nov 20, 2014 at 5:04
  • @ArunPJohny Thank you for the idea, I figured out that it's an error "parseerror" "unexpected token :" Commented Nov 20, 2014 at 14:39

2 Answers 2

2

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); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Thank you for the reply, but I wonder, what could be wrong? I checked the status of the call, and I got 200 and all the data in responseText.
@PhilippGrigoryev can you post your whole code in a fiddle?
i actually found a problem @ianaya89, see my answer below. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.