@clarabelle
To dynamically update a JSON-LD script with AJAX, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 |
$.ajax({ url: "your_api_endpoint", method: "GET", dataType: "json", success: function(data) { // Handle the retrieved data }, error: function(xhr, status, error) { // Handle the error } }); |
1 2 3 4 5 6 7 |
// Update the script tag using jQuery var script = $('script[type="application/ld+json"]'); script.html(JSON.stringify(data)); // Or update the script tag by manipulating the DOM var script = do***ent.querySelector('script[type="application/ld+json"]'); script.textContent = JSON.stringify(data); |
1 2 3 4 |
$(do***ent).ready(function() { // Retrieve the data and update the JSON-LD script // ... }); |
Note that you should only have one JSON-LD script tag on each page, so you may need to handle situations where the tag does not exist or where there are multiple tags.