0

I'm trying to retrieve the urls which are saved in all divs with the class "doi" under the data-value property by using the .each() function and pass it to a ajax call, so i can retrieve the specific data from the api and pass it as a string to specific div again.

Now i guess my problem here is, that the this pointer isn't working inside the ajax call. The answer might be quite simple i guess but i somehow can't figure it out.

jQuery(document).ready(function($) {
  $(".doi").each(function() {
    var doi = $(this).attr("data-value");
    $.ajax({
      headers: {
        Accept: "application/vnd.citationstyles.csl+json"
      },
      type: "GET",
      async: false,
      url: doi,
      success: function(data) {
        if (data) {
          $(this).text(JSON.stringify(data["is-referenced-by-count"]));
          //$(".doi").text(JSON.stringify(data["is-referenced-by-count"]));
        } else {
          $(this).text("no result...");
        }
      },
      error: function(jqXHR, textStatus, errorThrown) {
        $(this).text("error: " + textStatus);
      }
    });
  });
});
Meridol
  • 1
  • 1
  • Never ever use `async:false`. It is a terrible practice that can cause numerous UI issues and is deprecated. You should be seeing warnings about that in your dev tools console – charlietfl Jan 26 '18 at 19:50
  • Also make sure to use `dataType:'json'` – charlietfl Jan 26 '18 at 19:52
  • oh... yeah... async: false, i already deleted in my code, just tried it out to see the difference and forgot to delete it in my sample code. – Meridol Jan 26 '18 at 19:55
  • so my guess was right?.. unfortunately i have red the post already you are refering to before i was posting my question... but i still dont get it... can you maybe give me a more specific hint? – Meridol Jan 26 '18 at 20:54
  • ok... `this` is not the element in the callback. Several ways to fix it ... simple one is `context: this,` in options object – charlietfl Jan 26 '18 at 21:36
  • thank you sooo much... it works... get your donation link working again ;) – Meridol Jan 26 '18 at 22:15

0 Answers0