How To Elegantly Handle JSON Objects in Responses From Ajax Requests?
Asked Answered
R

4

5

I'm really new to using JSON to handle my Ajax Request and Response cycle. I've previously used just plain old parameters passed as POST data and I've rendered straight HTML in the response which was then placed into the DOM. As I've looked at various examples and read through various tutorials, it seems like a fairly common practice to simply build a string from the JSON object mixed with HTML that's been hard coded into the string and then assign the string as innerHTML to some element.

A common example looks something like this:

var jo = eval(req.responseText);

var strTxt = '<span>' + jo.f_name + ' ' + jo.l_name + '</span><br/>' + 'Your Age Is: ' + jo.age + '<br/>'; 

$('myDiv').innerHTML = strTxt;

Is there a more elegant (or correct) way of handling the JSON response so that I'm not hard coding HTML in the javascript? Or is this pretty much how people do it?

P.S. Links to tutorials or other sources are appreciated.

Ronnie answered 14/2, 2009 at 17:31 Comment(0)
C
2

I steer away from writing a lot of HTML within JavaScript strings. I prefer separation of structure from data manipulation. The better alternative is to place that code in the page, load the values based off the ID's, and show/hide it if necessary:

<div id="codeBlock" style="visible=false;">
    <span id="val1"></span>
    <br/>
    <span id="val2"></span>
    <br/>
</div>
............
<script>
    var jo = eval(req.responseText);
    $('val1').innerHTML = jo.f_name + ' ' + jo.l_name;
    $('val2').innerHTML = 'Your Age Is: ' + jo.age;
    $('codeBlock').show();
</script>

That might not be exactly what you want to do but you get the idea.

Censorship answered 14/2, 2009 at 17:50 Comment(4)
So in your structure, you have a separate function for each ajax call that is made?Ronnie
But I think that your suggestion may just be what I'm looking for.Ronnie
I think it would have been more appropriate for me to put "myDiv" instead of "codeBlock", to make more sense to your example (they would be the same thing).Censorship
Oh, and I don't know how to answer your first comment because I feel as if I do not know enough about your situation. Maybe start another question and give more details. :)Censorship
W
2

You could create the elements in the DOM using javascript instead of just dropping them into the innerHtml of the DIV, something like the following (untested):

var mySpan = document.createElement("span");
var spanContent = document.createTextNode(jo.f_name + ' ' + jo.l_name);
mySpan.appendChild(spanContent);
var myBr = document.createElement("br");
mySpan.appendChild(myBr);
var otherSpanContent = document.createTextNode('Your Age Is: ' + jo.age);
mySpan.appendChild(otherSpanContent);
mySpan.appendChild(myBr);

$('myDiv').appendChild(mySpan);
Wahlstrom answered 14/2, 2009 at 17:40 Comment(0)
C
2

I steer away from writing a lot of HTML within JavaScript strings. I prefer separation of structure from data manipulation. The better alternative is to place that code in the page, load the values based off the ID's, and show/hide it if necessary:

<div id="codeBlock" style="visible=false;">
    <span id="val1"></span>
    <br/>
    <span id="val2"></span>
    <br/>
</div>
............
<script>
    var jo = eval(req.responseText);
    $('val1').innerHTML = jo.f_name + ' ' + jo.l_name;
    $('val2').innerHTML = 'Your Age Is: ' + jo.age;
    $('codeBlock').show();
</script>

That might not be exactly what you want to do but you get the idea.

Censorship answered 14/2, 2009 at 17:50 Comment(4)
So in your structure, you have a separate function for each ajax call that is made?Ronnie
But I think that your suggestion may just be what I'm looking for.Ronnie
I think it would have been more appropriate for me to put "myDiv" instead of "codeBlock", to make more sense to your example (they would be the same thing).Censorship
Oh, and I don't know how to answer your first comment because I feel as if I do not know enough about your situation. Maybe start another question and give more details. :)Censorship
P
2

You could check out a templating engine such as PURE - may be a bit hard to get into at first but it supports many major javascript frameworks (and DOMAssistant which is nice) and separates html from the data.

Peon answered 14/2, 2009 at 17:53 Comment(0)
M
1

The objects created from JSON are standard Javascript objects, therefore you can easily use jQuery selectors to create or access DOM elements and insert content from your JSON objects.

eg.

// Create a new span element and set its text
var personSpan=$("<span>").text(jo.f_name + ' ' + jo.l_name);

// Append the span to the existing myDiv element
$("myDiv").append(personSpan);

// Create a new div element (better then br) and set its text
var personDiv=$("<div>").text("Your Age Is: " + jo.age);

// Append the new div to the existing myDiv element
$("myDiv").append(personDiv);
Marinamarinade answered 14/2, 2009 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.