Avatar billede Majsen Juniormester
09. februar 2018 - 23:06 Der er 7 kommentarer

API i JavaScript og html

Hej.
Jeg har prøvet at få et API fra New York Times ind i mit javascript.
Jeg skal gerne have henvist til det i en div i mit html document, og fortælle at jeg skal bruge f.eks. navn, forfatter, årstal (siden omhandler bøger).
Er der nogen der kan vise et eksempel på henvisning i html?
Her er den (meget lange) API tråd, og nederst, den div jeg har i mit html document.

{
  "status": "OK",
  "copyright": "Copyright (c) 2018 The New York Times Company.  All Rights Reserved.",
  "num_results": 31091,
  "results": [
    {
      "title": "\"I GIVE YOU MY BODY ...\"",
      "description": "The author of the Outlander novels gives tips on writing sex scenes, drawing on examples from the books.",
      "contributor": "by Diana Gabaldon",
      "author": "Diana Gabaldon",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Dell",
      "isbns": [
        {
          "isbn10": "0399178570",
          "isbn13": "9780399178573"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "0399178570",
          "primary_isbn13": "9780399178573",
          "rank": 8,
          "list_name": "Advice How-To and Miscellaneous",
          "display_name": "Advice, How-To & Miscellaneous",
          "published_date": "2016-09-04",
          "bestsellers_date": "2016-08-20",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "\"MOST BLESSED OF THE PATRIARCHS\"",
      "description": "A character study that attempts to make sense of Jefferson’s contradictions.",
      "contributor": "by Annette Gordon-Reed and Peter S. Onuf",
      "author": "Annette Gordon-Reed and Peter S Onuf",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Liveright",
      "isbns": [
        {
          "isbn10": "0871404427",
          "isbn13": "9780871404428"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "0871404427",
          "primary_isbn13": "9780871404428",
          "rank": 16,
          "list_name": "Hardcover Nonfiction",
          "display_name": "Hardcover Nonfiction",
          "published_date": "2016-05-01",
          "bestsellers_date": "2016-04-16",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 1,
          "dagger": 0
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "#ASKGARYVEE",
      "description": "The entrepreneur expands on subjects addressed on his Internet show, like marketing, management and social media.",
      "contributor": "by Gary Vaynerchuk",
      "author": "Gary Vaynerchuk",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "HarperCollins",
      "isbns": [
        {
          "isbn10": "0062273124",
          "isbn13": "9780062273123"
        },
        {
          "isbn10": "0062273132",
          "isbn13": "9780062273130"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "0062273124",
          "primary_isbn13": "9780062273123",
          "rank": 5,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2016-04-10",
          "bestsellers_date": "2016-03-26",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 1
        },
        {
          "primary_isbn10": "0062273124",
          "primary_isbn13": "9780062273123",
          "rank": 6,
          "list_name": "Advice How-To and Miscellaneous",
          "display_name": "Advice, How-To & Miscellaneous",
          "published_date": "2016-03-27",
          "bestsellers_date": "2016-03-12",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 1
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "#GIRLBOSS",
      "description": "An online fashion retailer traces her path to success.",
      "contributor": "by Sophia Amoruso",
      "author": "Sophia Amoruso",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Portfolio/Penguin/Putnam",
      "isbns": [
        {
          "isbn10": "039916927X",
          "isbn13": "9780399169274"
        },
        {
          "isbn10": "1591847931",
          "isbn13": "9781591847939"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "1591847931",
          "primary_isbn13": "9781591847939",
          "rank": 8,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2016-03-13",
          "bestsellers_date": "2016-02-27",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "1591847931",
          "primary_isbn13": "9781591847939",
          "rank": 9,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2016-01-17",
          "bestsellers_date": "2016-01-02",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "1591847931",
          "primary_isbn13": "9781591847939",
          "rank": 9,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2015-12-13",
          "bestsellers_date": "2015-11-28",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "1591847931",
          "primary_isbn13": "9781591847939",
          "rank": 8,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2015-11-15",
          "bestsellers_date": "2015-10-31",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "039916927X",
          "primary_isbn13": "9780399169274",
          "rank": 10,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2014-11-09",
          "bestsellers_date": "2014-10-25",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "039916927X",
          "primary_isbn13": "9780399169274",
          "rank": 8,
          "list_name": "Business Books",
          "display_name": "Business",
          "published_date": "2014-10-12",
          "bestsellers_date": "2014-09-27",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "039916927X",
          "primary_isbn13": "9780399169274",
          "rank": 14,
          "list_name": "Advice How-To and Miscellaneous",
          "display_name": "Advice, How-To & Miscellaneous",
          "published_date": "2014-09-21",
          "bestsellers_date": "2014-09-06",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "039916927X",
          "primary_isbn13": "9780399169274",
          "rank": 13,
          "list_name": "Advice How-To and Miscellaneous",
          "display_name": "Advice, How-To & Miscellaneous",
          "published_date": "2014-09-14",
          "bestsellers_date": "2014-08-30",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "$100 STARTUP",
      "description": "How to build a profitable start up for $100 or less and be your own boss.",
      "contributor": "by Chris Guillebeau",
      "author": "Chris Guillebeau",
      "contributor_note": "",
      "price": 23,
      "age_group": "",
      "publisher": "Crown Business",
      "isbns": [
        {
          "isbn10": "0307951529",
          "isbn13": "9780307951526"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "$20 PER GALLON",
      "description": "",
      "contributor": "by Christopher Steiner",
      "author": "Christopher Steiner",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Grand Central",
      "isbns": [],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "'57, Chicago",
      "description": null,
      "contributor": null,
      "author": "Steve Monroe",
      "contributor_note": null,
      "price": 0,
      "age_group": null,
      "publisher": null,
      "isbns": [
        {
          "isbn10": "0786867302",
          "isbn13": "9780786867301"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": null,
          "sunday_review_link": "https://www.nytimes.com/2001/07/29/books/books-in-brief-fiction-poetry-319660.html",
          "article_chapter_link": null
        }
      ]
    },
    {
      "title": "'ROCK OF AGES: ''ROLLING STONE'' HISTORY OF ROCK AND ROLL'",
      "description": null,
      "contributor": null,
      "author": "GEOFFREY STOKES, KEN TUCKER' 'ED WARD",
      "contributor_note": null,
      "price": 0,
      "age_group": null,
      "publisher": null,
      "isbns": [
        {
          "isbn10": "0671630687",
          "isbn13": "9780671630683"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": null,
          "sunday_review_link": "https://www.nytimes.com/1986/12/28/books/three-chord-music-in-a-three-piece-suit.html",
          "article_chapter_link": null
        }
      ]
    },
    {
      "title": "'THE HIGH ROAD TO CHINA: GEORGE BOGLE, THE PANCHEN LAMA AND THE FIRST BRITISH EXPEDITION TO TIBET'",
      "description": null,
      "contributor": null,
      "author": "KATE TELTSCHER",
      "contributor_note": null,
      "price": 0,
      "age_group": null,
      "publisher": null,
      "isbns": [
        {
          "isbn10": "0374217009",
          "isbn13": "9780374217006"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": null,
          "sunday_review_link": "https://www.nytimes.com/2007/04/22/books/review/Stuart.t.html",
          "article_chapter_link": null
        }
      ]
    },
    {
      "title": "'TIL DEATH",
      "description": "",
      "contributor": "by Sharon Sala",
      "author": "Sharon Sala",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Harlequin Mira",
      "isbns": [
        {
          "isbn10": "0778314278",
          "isbn13": "9780778314271"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "'TIL DEATH DO US PART",
      "description": "A matchmaker in Victorian England turns to a crime novelist for help when she starts receiving a series of disturbingly personalized trinkets.",
      "contributor": "by Amanda Quick",
      "author": "Amanda Quick",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Berkley",
      "isbns": [
        {
          "isbn10": "069819361X",
          "isbn13": "9780698193611"
        },
        {
          "isbn10": "039917446X",
          "isbn13": "9780399174469"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "069819361X",
          "primary_isbn13": "9780698193611",
          "rank": 15,
          "list_name": "Combined Print and E-Book Fiction",
          "display_name": "Combined Print & E-Book Fiction",
          "published_date": "2016-05-08",
          "bestsellers_date": "2016-04-23",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "069819361X",
          "primary_isbn13": "9780698193611",
          "rank": 9,
          "list_name": "E-Book Fiction",
          "display_name": "E-Book Fiction",
          "published_date": "2016-05-08",
          "bestsellers_date": "2016-04-23",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "'Til Faith Do Us Part: How Interfaith Marriage is Transforming America",
      "description": null,
      "contributor": null,
      "author": "Naomi Schaefer Riley",
      "contributor_note": null,
      "price": 0,
      "age_group": null,
      "publisher": null,
      "isbns": [
        {
          "isbn10": "0199873747",
          "isbn13": "9780199873746"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": null,
          "sunday_review_link": "https://www.nytimes.com/2013/05/26/books/review/til-faith-do-us-part-by-naomi-schaefer-riley.html",
          "article_chapter_link": null
        }
      ]
    },
    {
      "title": "'TIS THE SEASON",
      "description": "Two classic holiday stories — \"Under the Christmas Tree\" (2009) and \"Midnight Confessions\" (2010) — plus the novella \"Backward Glance\" (1991).",
      "contributor": "by Robyn Carr",
      "author": "Ron Carr",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Harlequin Mira",
      "isbns": [
        {
          "isbn10": "0778316645",
          "isbn13": "9780778316640"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "0778316645",
          "primary_isbn13": "9780778316640",
          "rank": 18,
          "list_name": "Mass Market Paperback",
          "display_name": "Paperback Mass-Market Fiction",
          "published_date": "2014-11-30",
          "bestsellers_date": "2014-11-15",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "0778316645",
          "primary_isbn13": "9780778316640",
          "rank": 6,
          "list_name": "Mass Market Paperback",
          "display_name": "Paperback Mass-Market Fiction",
          "published_date": "2014-11-16",
          "bestsellers_date": "2014-11-01",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 1,
          "dagger": 0
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "------, THAT'S DELICIOUS",
      "description": "",
      "contributor": "by Action Bronson with Rachel Wharton",
      "author": "Action Bronson with Rachel Wharton",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Abrams",
      "isbns": [
        {
          "isbn10": "1419726552",
          "isbn13": "9781419726552"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "1419726552",
          "primary_isbn13": "9781419726552",
          "rank": 9,
          "list_name": "Advice How-To and Miscellaneous",
          "display_name": "Advice, How-To & Miscellaneous",
          "published_date": "2017-10-01",
          "bestsellers_date": "2017-09-16",
          "weeks_on_list": 1,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 1
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "...and the Horse He Rode In On: The People V. Kenneth Starr",
      "description": null,
      "contributor": null,
      "author": "James Carville",
      "contributor_note": null,
      "price": 0,
      "age_group": null,
      "publisher": null,
      "isbns": [
        {
          "isbn10": "0684857340",
          "isbn13": "9780684857343"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": null,
          "sunday_review_link": "https://www.nytimes.com/1998/10/18/books/preaching-to-the-converted.html",
          "article_chapter_link": null
        }
      ]
    },
    {
      "title": ".HACK G.U.  , VOL. 5",
      "description": "This series, set in the future, is about an online, multiplayer game run amok. This volume concludes the tale.",
      "contributor": "by Hamazaki Tatsuya",
      "author": "Hamazaki Tatsuya",
      "contributor_note": "",
      "price": 10.99,
      "age_group": "",
      "publisher": "TOKYOPOP",
      "isbns": [],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "1 Ragged Ridge Road",
      "description": null,
      "contributor": null,
      "author": "David Adams Richards",
      "contributor_note": null,
      "price": 0,
      "age_group": null,
      "publisher": null,
      "isbns": [
        {
          "isbn10": "0671003542",
          "isbn13": "9780671003548"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": null,
          "sunday_review_link": "https://www.nytimes.com/1997/08/31/books/books-in-brief-fiction-747866.html",
          "article_chapter_link": null
        }
      ]
    },
    {
      "title": "1,000 PLACES TO SEE BEFORE YOU DIE",
      "description": "A guide for traveling the world; second edition updated with new entries.",
      "contributor": "by Patricia Schultz",
      "author": "Patricia Schultz",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Workman",
      "isbns": [
        {
          "isbn10": "0761156860",
          "isbn13": "9780761156864"
        },
        {
          "isbn10": "0761104844",
          "isbn13": "9780761104841"
        }
      ],
      "ranks_history": [
        {
          "primary_isbn10": "0761156860",
          "primary_isbn13": "9780761156864",
          "rank": 10,
          "list_name": "Travel",
          "display_name": "Travel",
          "published_date": "2015-04-12",
          "bestsellers_date": "2015-03-28",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "0761156860",
          "primary_isbn13": "9780761156864",
          "rank": 8,
          "list_name": "Travel",
          "display_name": "Travel",
          "published_date": "2015-03-15",
          "bestsellers_date": "2015-02-28",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        },
        {
          "primary_isbn10": "0761156860",
          "primary_isbn13": "9780761156864",
          "rank": 12,
          "list_name": "Travel",
          "display_name": "Travel",
          "published_date": "2015-01-11",
          "bestsellers_date": "2014-12-27",
          "weeks_on_list": 0,
          "ranks_last_week": null,
          "asterisk": 0,
          "dagger": 0
        }
      ],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "1,000 RECORDINGS TO HEAR BEFORE YOU DIE",
      "description": "Operas, rock albums, blues, classical music, world music and other essentials, from a musician and music journalist.",
      "contributor": "by Tom Moon",
      "author": "Tom Moon",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Workman",
      "isbns": [],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    },
    {
      "title": "1,227 QUITE INTERESTING FACTS TO BLOW YOUR SOCKS OFF",
      "description": "A compilation of interesting information on subjects ranging from Zeus to Zuckerberg.",
      "contributor": "by John Lloyd and others",
      "author": "John Lloyd and others",
      "contributor_note": "",
      "price": 0,
      "age_group": "",
      "publisher": "Norton",
      "isbns": [
        {
          "isbn10": "0393241033",
          "isbn13": "9780393241037"
        }
      ],
      "ranks_history": [],
      "reviews": [
        {
          "book_review_link": "",
          "first_chapter_link": "",
          "sunday_review_link": "",
          "article_chapter_link": ""
        }
      ]
    }
  ]
}

Javascript side!

      $.getJSON("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json", {'api-key':"3cab252d9bf94339976a208a1e9ce1ea"}, function(data) {
        var results = data.results; // data.results samler alt data fra vores API

       
        for (var i = 0; i < results.length; i++){
         
          var article = results[i];
          var title = article.display_title;
          var description = article.description;
          var contributor = article.contributor;
          var author = article.author;
          var contributor_note = article.contributor_note;
          var price = article.price;
          var age_group = article.age_group;
          var publisher = article.publisher;
         
         
          var addAPIS = new Post(title, description, contributor, author, contributor_note, price, age_group, publisher);
Avatar billede jakobdo Ekspert
10. februar 2018 - 08:20 #1
Hvad jeg lige hurtigt kan se, så bør du have alt data i dette data:

for (var i = 0; i < results.length; i++){
         
          var article = results[i];
          var title = article.display_title;
          var description = article.description;
          var contributor = article.contributor;

Prøv at "print" dem...

var title = article.display_title;
console.log("Title: " + title")
var author = article.author;
console.log("Author: " + author")

og se om ikke det er hvad du skal bruge.
Avatar billede Majsen Juniormester
10. februar 2018 - 10:00 #2
Hmm.. det virker ikke umiddelbart.
Kan du se hvorfor den viser en fejl ved den her linje
$.getJSON("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json", {'api-key':"3cab252d9bf94339976a208a1e9ce1ea"}, function(data) {
        var results = data.results;
Avatar billede olsensweb.dk Ekspert
10. februar 2018 - 13:54 #3
#2
hvilke browser bruger du ??

du får et dataset retur som forvendted.


jf. chrome med webdev tools som add_on får jeg denne fejl
Uncaught ReferenceError: Post is not defined
det er denne linje
var addAPIS = new Post(title, description,
den brokker sig over.
hvor er classen post henne ??

hvis man udkommenterer linjen brokker den sig også over
var title = article.display_title;
den skal være
var title = article.title;

man kan læse doc her https://developer.nytimes.com/books_api.json

kan du ikke lave en online demo, og komme med et link


spm kører også her https://www.computerworld.dk/eksperten/spm/1021869
Avatar billede jakobdo Ekspert
10. februar 2018 - 17:15 #4
Her bruger jeg noget jquery og mustache.
Se denne kode:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>NYTimes Api Test</title>
  <meta name="description" content="The HTML5 Herald">
</head>

<body>
  <script id="template" type="x-tmpl-mustache">
    {{#results}}
      <b>{{title}}</b><br>
      {{author}}<br>
      {{description}}<br>
      <hr>
    {{/results}}
  </script>
  <div id="output"></div>
  <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/2.3.0/mustache.min.js"></script>
  <script type="text/javascript">
    let api_url = "https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json";
    let api_key = "3cab252d9bf94339976a208a1e9ce1ea";
    $(function(){
      $.getJSON(api_url, {'api-key':api_key}, function(data) {
        console.log(data);
        var template = $('#template').html();
        Mustache.parse(template); // optional, speeds up future uses
        var rendered = Mustache.render(template, {results: data.results});
        $('#output').html(rendered);
      });
    });
  </script>
</body>
</html>
Avatar billede Majsen Juniormester
10. februar 2018 - 17:33 #5
Tak jacobdo, men kender ikke rigtig mustache
Avatar billede jakobdo Ekspert
10. februar 2018 - 18:05 #6
Mustache er en smart måde at tilføje html i din nuværende struktur - https://github.com/janl/mustache.js/
Avatar billede olsensweb.dk Ekspert
10. februar 2018 - 18:10 #7
#5
kig på dette, tæt på dit udgangspunkt

<!DOCTYPE html>
<html lang="da">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
        <title>demo</title>

        <style type="text/css">
        </style>
        <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>


        <script type="text/javascript">
            $(document).ready(function () {

                $.getJSON("https://api.nytimes.com/svc/books/v3/lists/best-sellers/history.json", {'api-key': "3cab252d9bf94339976a208a1e9ce1ea"}, function (data) {
                    var results = data.results; // data.results samler alt data fra vores API

                    var str = "";
                    for (var i = 0, lng = results.length; i < lng; i++) {
                        var article = results[i];
                        var title = article.title;
                        var description = article.description;
                        //var contributor = article.contributor;
                        var author = article.author;
                        //var contributor_note = article.contributor_note;
                        //var price = article.price;
                        //var age_group = article.age_group;
                        //var publisher = article.publisher;
                        str += "<b>" + title + "</b><br>";
                        str += author + "<br>";
                        str += description + "<br>";
                        str += "<hr>";
                    }
                    $('#mydiv').html(str);
                    //document.getElementById("mydiv").innerHTML = str;
                })
            })
        </script>
    </head>
    <body>
        <div id="mydiv"> <!-- indhold kommer via js --> </div>


    </body>
</html>
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester