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

  • SEARCH
  • Home
  • 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 8683093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:57:06+00:00 2026-06-12T21:57:06+00:00

I have a jQuery Ajax call to my server to retrieve some HTML for

  • 0

I have a jQuery Ajax call to my server to retrieve some HTML for a section on my page based on some user activity with my site. I’m finding I need in addition to the rendered HTML from the server some JSON data. What’s best way to return to the client rendered HTML and JSON data? I’m thinking there are 3 different options:

  1. make a second call to the server for the JSON data. This involves a second round trip. I can string multiple ajax calls using:

    $when(call_1, call_2).done(function(results_1, results_2){…}))

  2. include a script block in the rendered html that is called once the rendered html is added to the dom:

    ... rendered html output ...
    $(document).ready(function ()
    {
        alert('data here');
    });
    
  3. somehow embed rendered html into json returned from call then have calling JS function separate the rendered html from JSON and update DOM accordingly. This option just smells bad.

I’m leaning to option 1 even though it’s an additional server hit because it seems like a better approach. Which approach is better in your opinion? Is there another way I’ve not thought of?

Thanks

  • 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-12T21:57:08+00:00Added an answer on June 12, 2026 at 9:57 pm

    First of all, encode your HTML on server side.. something like:

    function HTMLEscape(str) {   //Javascript code, adjust as per your server-side lang
        return String(str)
            .replace(/&/g, '&')
            .replace(/"/g, '"')
            .replace(/'/g, ''')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
    }
    

    Now, supposedly you received the following JSON response at client-side:

    data = {
         html: "&lt;div&gt;Text goes here&lt;/div&gt;&lt;script type=&#39;text/javascript&#39;&gt;alert(&#39;text goes here too&#39;)&lt;/script&gt;",
         status: 1,
         some: 'stuff'
    };
    

    On Ajax complete, start unpacking your JSON:

    ResultJSON = data;
    
    $("div#destination").html(HTMLUnescape(ResultJSON.html));
    $("input#status").val(ResultJSON.status)
    

    Finally, hide this Javascript function somewhere in your .js file:

    function HTMLUnescape(str) {
        return String(str)
            .replace(/&amp;/g, '&')
            .replace(/&quot;/g, '"')
            .replace(/&#39;/g, "'")
            .replace(/&lt;/g, '<')
            .replace(/&gt;/g, '>');
    }
    

    Hope it helps. (-:

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

Sidebar

Related Questions

I have a jQuery ajax call that returns html of a table. Now I
On my main page I have this jquery code which does an ajax call
I'm using jQuery AJAX to retrieve a page with different user selected options. I'd
I have the following JQuery AJAX call that sends HTML form data to my
I have html.actionlink in my asp.net MVC 3view and jquery ajax call on link
Ok, so I have a JQuery AJAX call in an .html file, sitting on
I have a jQuery AJAX call that I will need to do multiple times
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
A situation I ran across this week: we have a jQuery Ajax call that
I have a jquery json ajax call and inside my success function I have

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.