Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:35:25+00:00 2026-06-09T10:35:25+00:00

I’m using varnish+esi to return external json content from a RESTFul API. This technique

  • 0

I’m using varnish+esi to return external json content from a RESTFul API.
This technique allows me to manage request and refresh data without using webserver resources for each request.

e.g:

<head>
.... 
<script> 
 var data = <esi:include src='apiurl/data'>;
</script>
...

After include the esi varnish will return:

 var data = {attr:1, attr2:'martin'};

This works fine, but if the API returns an error, this technique will generate a parse error.

var data = <html><head><script>...api js here...</script></head><body><h1 ... api html ....

I solved this problem using a hidden div to parse and catch the error:

...
<b id=esi-data style=display:none;><esi:include src='apiurl/data'></b>
<script>
  try{
    var data = $.parseJSON($('#esi-data').html());
  }catch{ alert('manage the error here');}
....

I’ve also tried using a script type text/esi, but the browser renders the html inside the script tag (wtf), e.g:

<script id=esi-data type='text/esi'><esi:include src='apiurl/data'></script>

Question:

Is there any why to wrap the tag and avoid the browser parse it ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:35:27+00:00Added an answer on June 9, 2026 at 10:35 am

    Let me expand upon the iframe suggestion I made in my comment—it’s not quite what you think!

    The approach is almost exactly the same as what you’re doing already, but instead of using a normal HTML element like a div, you use an iframe.

    <iframe id="esi-data" src="about:blank"><esi:include src="apiurl/data"></iframe>
    
    var $iframe = $('#esi-data');
    
    try {
        var data = $.parseJSON($iframe.html());
    } catch (e) { ... }
    
    $iframe.remove();
    
    #esi-data { display: none; }
    

    How is this any different from your solution? Two ways:

    1. The data/error page are truly hidden from your visitors. An iframe has an embedded content model, meaning that any content within the <iframe>…</iframe> tags gets completely replaced in the DOM—but you can still retrieve the original content using innerHTML.

    2. It’s valid HTML5… sort-of. In HTML5, markup inside iframe elements is treated as text. Sure, you’re meant to be able to parse it as a fragment, and it’s meant to contain only phrasing content (and no script elements!), but it’s essentially just treated as text by the validator—and by browsers.

    3. Scripts from the error page won’t run. The content gets parsed as text and replaced in the DOM with another document—no chance for any script elements to be processed.

    Take a look at it in action. If you comment out the line where I remove the iframe element and inspect the DOM, you can confirm that the HTML content is being replaced with an empty document. Also note that the embedded script tag never runs.

    Important: this approach could still break if the third party added an iframe element into their error page for some reason. Unlikely as this may be, you can bulletproof the approach a little more by combining your technique with this one: surround the iframe with a hidden div that you remove when you’re finished parsing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.