-
EJS Sending Json Object to Template
Greetings,
I have a page that uses Jquery to get some Json encoded data from a separate script. When that data returns I want to send it to my template page and loop through the data to populate one or more forms, depending on number of records in the json data.
Sending code in html page (in part)
<script type="text/javascript">
/* some code here - not needed to show */
$("#filter-results").empty().append("back from hours-get.php");
var result = new EJS({url: 'hours-form.ejs'}).render('data');
/* some code here - not needed to show */
</script>
This code works. I'm getting data back as a Json object. My problem is I do not know how to loop through "data" on the hours-form.ejs page using the <% ... %> tags. I've tried using jquery's " $.each(data, function(i,item){ " code but I get errors.
Would anyone know how I should send 'data' to hours-form.ejs and then loop through it?
-
I gottit!!
I downloaded jason2.js and added it to my code (included) from http://www.json.org/json2.js. To my html file I added:
items = JSON.stringify(data);
This takes a JSON object and converts it to an array (items).
In my hours-form.ejs I then loop through the items array.
Code:
<script type="text/javascript">
$(document).ready(function(){
$("#filter-results").html("Search Results will appear here!.");
$("#job_id").bind("change", function(e){
$.getJSON("hours-get.php", {job_id:$("#job_id").val()}, function(data){
items = JSON.stringify(data);
var result = new EJS({url: 'hours-form.ejs'}).render('items');
document.getElementById('filter-results').innerHTML = result;
});
});
});
</script>
I'm good now.
Thanks,
-
Welcome to DevX 
Thanks for posting your solution. It might help others in the same, or similiar, situation.
Similar Threads
-
By JULDUZ in forum ASP.NET
Replies: 0
Last Post: 08-29-2002, 09:40 AM
-
By John Rigsby in forum ASP.NET
Replies: 1
Last Post: 10-03-2001, 09:39 AM
-
By Jaco de Villiers in forum XML
Replies: 1
Last Post: 06-01-2001, 05:50 PM
-
Replies: 0
Last Post: 05-01-2001, 02:46 PM
-
By Greg Dirst in forum authorevents.appleman
Replies: 1
Last Post: 04-10-2000, 02:56 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|