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

2 Answers 2

1

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. (-:

Sign up to request clarification or add additional context in comments.

Comments

0

option 1 is better. You may also think of this,

Let the json dictionary sent to the html contain all your data and the html content. Something like this,

{
'html':"<div>this is new html</div>",
'var1':'var1 value',
'var2':'var2 value'
}

1 Comment

Thanks for your feedback. I thought about what you describe. My rendered HTML can be rather large and I'm concerned that some a funny character in the rendered html may make the JSON invalid. Thoughts?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.